我遇到的问题情景是在侧边栏菜单,相同路由配置仅仅是参数不同,页面不会刷新。
以下只列出了三种方法
我采用的是这一种方法,只需要在这一个地方修改就行,设置router-view中的key属性,附带上参数值
<router-view :key="$route.name + ($route.params.id || '')"></router-view>
还有一种是和上面这个比较像,使用vue文档组件绑定的key值来进行强制刷新,只设置需要用到的组件的key属性
<MyComponent :key="routeParams" />
还有一种是直接使用watch监听路由$route对象属性的变化
watch: {
'$route' (to, from) {
//操作
}
}
最后一种就是为相同路由页面的跳转进行中间路由替换,在router上注册beforeEach全局守卫进行拦截,跳转到一个中间路由(例如empty),再从中间过渡路由跳转至要去的路由。
// 全局导航守卫
router.beforeEach((to, from, next) => {
if (to.name === from.name && to.params.type !== from.params.type) {
next({name: 'empty', query: {toPath: to.fullPath}})
} else {
next()
}
})
// 中间过渡路由
let toPath = this.$route.query.toPath
if (this.toPath) {
this.$router.push({path: this.toPath})
}