this.$router.push
1.跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 2.声明式 <router-link :to="...."> 编程式 router.push(...)//该方法的参数可以是一个字符串路径,或者一个描述地址的对象。
1 this.$router.push({path:'/index'}) 2 this.$router.push({path:'/index',query:{name: '123'}}) 3 this.$router.push({name:'index',params:{name:'123'}}) 4 5 // 字符串 6 7 router.push('home') 8 // 对象 9 this.$router.push({path: '/login?url=' + this.$route.path}); 10 // 带查询参数,变成/backend/order?selected=2 11 this.$router.push({path: '/backend/order', query: {selected: "2"}}); 12 // 命名的路由 13 router.push({ name: 'user', params: { userId: 123 }})
this.$router.replace
1.跳转到指定URL,替换history栈中最后一个记录,点击后退会返回至上上一个页面 (A----->B----->C 结果B被C替换 A----->C) 2.设置 replace 属性(默认值: false)的话,当点击时,会调用 router.replace() 而不是 router.push(),于是导航后不会留下 history 记录。即使点击返回按钮也不会回到这个页面 加上replace: true后,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。
1 <router-link :to="..." replace></router-link> 2 // 编程式: 3 router.replace(...) 4 //push方法也可以传replace 5 this.$router.push({path: '/home', replace: true})
两种传参方式
1 this.$router.push({ 2 path: '/home', 3 query: { 4 site: [], 5 bu: [] 6 } 7 }) 8params: 9 this.$router.push({ 10 name: 'Home', // 路由的名称 11 params: { 12 site: [], 13 bu: [] 14 } 15 }) 16params:/router1/:id ,/router1/123,/router1/789 ,这里的id叫做params 17query:/router1?id=123 ,/router1?id=456 ,这里的id叫做query。
this.$router.go(n)
1.类似window.history.go(n),向前或向后跳转n个页面,n可正(先后跳转)可负(向前跳转) 2.this.$router.go(1) //类似history.forward() this.$router.go(-1) //类似history.back()
