2025年vue跳转页面的几种常用方法

vue跳转页面的几种常用方法目录 vue 跳转不同页面的方法 1 router link 跳转 2 this router push 3 a 标签可以跳转外部链接 不能路由跳转 vue 三种不同方式实现跳转页面 Vue router link this router push this router go 1

大家好,我是讯享网,很高兴认识大家。

目录

vue跳转不同页面的方法

1.router-link跳转

2.this.$router.push()

3.a标签可以跳转外部链接,不能路由跳转

vue三种不同方式实现跳转页面

this.$router.push("/")

this.$router.go(1)

代码示例:


讯享网

vue跳转不同页面的方法

1.router-link跳转

<!-- 直接跳转 -->
<router-link to='/testC'>
 <button>点击跳转2</button>
</router-link>
 
<!-- 带参数跳转 -->
<router-link :to="{path:'testC',query:{setid:}}">
 <button>点击跳转1</button>
</router-link>
 
<router-link :to="{name:'testC',params:{setid:}}">
 <button>点击跳转3</button>
</router-link>

2.this.$router.push()

<template>
 <div id='app'>
 <button @click='goTo()'>点击跳转4</button>
 </div>
</template>
<script>
 new Vue({
 el:'#app',
 methods:{
 goTo(){
 //直接跳转
 this.$router.push('/testDemo');
 //带参数跳转
 this.$router.push({path:'/testC',query:{setid:}});
 this.$router.push({name:'testC',params:{setid:}});
 }
 }
 })
</script>

3.a标签可以跳转外部链接,不能路由跳转

<a href="https://www.baidu.com"><button>点击跳转5</button></a>

接收:this.$route.query.serid 和 this.$route.params.setid

vue三种不同方式实现跳转页面

 <router-link to="/">[跳转到主页]</router-link>
 <router-link to="/login">[登录]</router-link>
 <router-link to="/logout">[登出]</router-link>
this.$router.push("/");

this.$router.push("/")

<button @click="goHome">[跳转到主页]</button>
export default {
 name: "App",
 methods: {
 // 跳转页面方法
 goHome() {
 this.$router.push("/");
 },
}

this.$router.go(1)

 <button @click="upPage">[上一页]</button>
 <button @click="downPage">[下一页]</button>
 upPage() {
 // 后退一步记录,等同于 history.back()
 this.$router.go(-1);
 },
 downPage() {
 // 在浏览器记录中前进一步,等同于 history.forward()
 this.$router.go(1);
 }
 

代码示例:

<template>
 <div id="app">
 <img src="./assets/logo.png">
 <router-view/>
 <router-link to="/">[跳转到主页]</router-link>
 <router-link to="/login">[登录]</router-link>
 <router-link to="/logout">[登出]</router-link>
 
 <!-- javascript跳转页面 -->
 <button @click="goHome">[跳转到主页]</button>
 
 <!-- 回到上一页 -->
 <button @click="upPage">[上一页]</button>
 <button @click="downPage">[下一页]</button>
 
 <!-- 回到下一页 -->
 
 </div>
</template>
 
<script>
 export default {
 name: "App",
 methods: {
 // 跳转页面方法
 goHome() {
 this.$router.push("/");
 },
 upPage() {
 // 后退一步记录,等同于 history.back()
 this.$router.go(-1);
 },
 downPage() {
 // 在浏览器记录中前进一步,等同于 history.forward()
 this.$router.go(1);
 }
 }
 };
</script>

小讯
上一篇 2025-01-16 09:53
下一篇 2025-02-26 16:33

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/61268.html