1. 作用
不借助<router-link>
实现路由跳转,让路由跳转更加灵活。(比如可以写在某个按钮中,点击按钮后实现路由的跳转)
2. 具体编码
//$router的两个API
//一般情况下路由默认使用push。
this.$router.push({
name:'xiangqing',
params:{
id:xxx,
tittle:xxx
}
})
//如果不通过router,而是直接在路由组件中使用,就在router-link标签内使用replace。
this.$router.replace({
name:'xiangqing',
params:{
id:xxx,
tittle:xxx
}
})
this.$router.forward() //前进
this.$router.back() //后退
this.$router.go() //可前进也可后退,根据参数的正负数值确定前进或者后退的次数
注:编程式路由导航跳转需要写在父级路由里面,而不是跳转路由的里面!