vue的router学习笔记2之路由编程式导航

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40283784/article/details/87284770

1.router.push(对应window下的 window.history.pushState

const userId = '123'
router.push({ name: 'user', params: { userId }}) // -> /user/123
router.push({ path: `/user/${userId}` }) // -> /user/123

注意:path和那么不能同时使用,同样的规则也适用于ro。

2.router.replace(对应window下的 window.history.replaceState

router.replace(location, onComplete?, onAbort?)

跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。

3.router.go(对应window下的window.history.go

这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 

this.$router.go(n)

this.$router.go(1)//前进一步
this.$router.go(-1)//后退一步

//如果history的数量不够用,就会默默地失败

参考:https://router.vuejs.org/zh/guide/essentials/navigation.html

猜你喜欢

转载自blog.csdn.net/qq_40283784/article/details/87284770