1.this.$router
是VueRouter的一个对象,表示全局路由器对象,项目中通过router路由参数注入路由之后,在任何一个页面都可以通过此方法获取到路由器对象,并调用其push(), go()等方法
2.this.$route
表示当前正在用于跳转的路由器对象,可以调用其name、path、query、params等方法
push和replace区别
$router.push({path:'home'});本质是向history栈中添加一个路由,在我们看来是切换路由,但本质是在添加一个history记录
$router.replace({path:'home'});替换路由,没有历史记录,点击返回,会跳转到上上一个页面
push 传递参数和接收参数
字符串
this.$router.push(name)
this.$router.push(path)
对象
传参:name搭配params (类似post)
this.$router.push({
name:'xxx',
params:{
id:id
}
})
接收参数:
this.$route.params.id
传参: path搭配query (类似get)
this.$router.push({
path:'/xxx',
query:{
id:id
}
})
接收参数:
this.$route.query.id
replac的使用方法
this.$router.replace({ path: "/" })//Vue Router 的 replace() 方法来替换当前的路由。这个方法会把当前的路由从历史记录中移除,并将新的路由加入到历史记录中。这样用户就不能通过浏览器的后退按钮来返回到之前的页面。