关于this.$router.push、replace、go的用法和区别
1、this.$router.push跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面在这里插入代码片// 字符串this.$router.push('index')// 对象this.$router.push({path: 'login-pw'})// 带参数this.$router.push({path: 'login-pw', query: {'accou
1、this.$router.push
this.$router.push
跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面
1.跳转到指定url路径,并想history栈中添加一个记录,点击后退会返回到上一个页面
2.声明式:< router-link :to = “…” >
3.编程式:< router.push(…) > // 该方法的参数可以是一个字符串路径,或者一个描述地址的对象
在这里插入代码片// 字符串
this.$router.push('index')
// 对象
this.$router.push({path: 'login-pw'})
// 带参数
this.$router.push({path: 'login-pw', query: {'account': this.account.account}})
// 跳转后的页面获取参数
this.account.account = this.$route.query.account
2、this.$router.replace
跳转到指定url路径,但是history栈中不会有记录,点击返回会跳转到上上个页面
1.跳转到指定的URL,替换history栈中最后一个记录,点击后退会返回至上一个页面。(A----->B----->C 结果B被C替换 A----->C)
2.设置replace属性(默认值:false)的话,当点击时,会调用router.replace(),而不是router.push(),于是导航后不会留下history记录。
3.即使点击返回按钮也不会回到这个页面。加上replace: true时,它不会向 history 添加新纪录,而是跟它的方法名一样——替换当前的history记录。
// 声明式
<reouter-link :to="..." replace></router-link>
// 编程式:
router.replace(...)
// push方法也可以传replace
this.$router.push({path: '/homo', replace: true})
this.$router.replace({
name: this.pageFrom,
params: this.formData
})
onConfirm: () => {
this.$router.replace('/TravelManage')
}
3、this.$router.go(n)
向前或者向后跳转n个页面,n可为正整数或负整数
1.向前或者向后跳转n个页面,n可为正整数或负整数
2.this.$router.go(1) // 类似history.forward()
3.this.$router.go(-1) // 类似history.back()
更多推荐
所有评论(0)