nuxt.js中如何使用watch监听路由


```javascript
watch: {
    $route: {
      immediate: true,    //加上此配置之后,watch即可以在首次进入或刷新之后执行handler (),即初始化即可执行监听
      handler (to, from) {  //监听之后执行的回调
        if (to.path === '/login-pc' && this.allConfig.enable_login !== '1') {    //判断条件
          this.$router.push('/pc')
        }
        if (
          to.path === '/pc/register' &&
          this.allConfig.enable_register !== '1'
        ) {
          this.$router.push('/pc')
        }
      }
    }
  },

**总结:nuxt.js中其实和vue中的watch监听路由是一样的。没什么区别。**

猜你喜欢

转载自blog.csdn.net/qq_46634851/article/details/123186810
今日推荐