vue页面跳转

1、router-link跳转

复制代码
1.不带参数
<router-link :to="{name:'home'}"> 
<router-link :to="{path:'/home'}">

 
2.带params参数
<router-link :to="{name:'home', params: {id:123456}}"> 

 
3.带query参数
<router-link :to="{name:'home', query: {id:123456}}"> 

2、this.$router.push()

复制代码
1. 不带参数
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
 
2. query传参 
this.$router.push({name:'home',query: {id:'123456'}})
this.$router.push({path:'/home',query: {id:'123456'}})

 
3. params传参
this.$router.push({name:'home',params: {id:'123456'}})

跳转到指定url路径,并向history栈中添加一个记录,点击后退会返回到上一个页面。

query和params区别

query类似get, 跳转之后页面url后面会拼接参数,类似?id=123456, 非重要性的可以这样传, 密码之类还是用params刷新页面id还在

params类似post, 跳转之后页面url后面不会拼接参数, 但是刷新页面id会消失。

3、this.$router.replace()

用法同上,和第2个的this.$router.push方法一样。

跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上个页面 (直接替换当前页面)。

4、this.$router.go(n)

复制代码
<button @click="upPage">上页</button>
<button @click="downPage">下页</button>


// 上页
upPage() {
    this.$router.go(-1);  // 后退一步记录,等同于 history.back()
},

// 下页
downPage() {
    this.$router.go(1);   // 在浏览器记录中前进一步,等同于 history.forward()
}

向前或者向后跳转n个页面,n可为正整数或负整数。

相关推荐
Mr.Jessy1 天前
JavaScript高级:构造函数与原型
开发语言·前端·javascript·学习·ecmascript
爱上妖精的尾巴1 天前
6-4 WPS JS宏 不重复随机取值应用
开发语言·前端·javascript
三七吃山漆1 天前
攻防世界——wife_wife
前端·javascript·web安全·网络安全·ctf
用户47949283569151 天前
面试官问"try-catch影响性能吗",我用数据打脸
前端·javascript·面试
GISer_Jing1 天前
前端营销技术实战:数据+AI实战指南
前端·javascript·人工智能
嘉琪0011 天前
Vue3+JS 高级前端面试题
开发语言·前端·javascript
vipbic1 天前
用 Turborepo 打造 Strapi 插件开发的极速全栈体验
前端·javascript
天涯学馆1 天前
为什么 JavaScript 可以单线程却能处理异步?
前端·javascript
JIngJaneIL1 天前
基于java + vue校园快递物流管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js
asdfg12589631 天前
JS中的闭包应用
开发语言·前端·javascript