vue router-view 切换路由默认不触发钩子函数

1,746 阅读1分钟

真实的业务场景中,这种情况很多。比如

页面切换时不会触发mounted钩子,解决方法在router-view上加上一个唯一的key,来保证路由切换时都会重新渲染触发钩子了。

 <router-view :key="key"></router-view>

: computed: {
    key() {
        return this.$route.name !== undefined? this.$route.name + +new Date(): this.$route + +new Date()
    }
}

前端菜鸟写笔记(非原创)欢迎点赞。