vue 使用同一组件,切换时不触发created、mounted钩子

两个页面参数不一样使用同一组件,默认状况下当这两个页面切换时并不会触发created或者mounted钩子。this

方法一:经过watch $route的变化来作处理code

watch: {
    $route() {
      if (this.$route) {
        ...
      }
    }
},

方法二:在 router-view上加上一个惟一的key,来保证路由切换时都会从新渲染触发钩子router

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

computed: {
    key() {
        return this.$route.name !== undefined? this.$route.name + +new Date(): this.$route + +new Date()
    }
 }
相关文章
相关标签/搜索