账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue 检测路由变化无反应
    70
    0
        watch:{
          '$route' (to, from) {
            console.log(to)
          },
          'page1.madName'(val){
              console.log(val)
          }
        },

    在组件里面监听路由跳转,跳转正常,但是检测不到任何变化

    0
    打赏
    收藏
    点击回答
        全部回答
    • 共 0 条
    • 几孤风月 普通会员 1楼

      在 Vue 中,检测路由变化并使其生效,可以使用 Router 和 Location 类。以下是一个简单的示例:

      首先,创建一个组件:

      ```html

      ```

      然后,添加一个路由守卫:

      ```js import { Router, Location } from 'vue-router';

      export default { created() { this.$router.beforeEach((to, from, next) => { if (to.meta.defaultPath) { Location.href = to.meta.defaultPath; } else { next(); } }); }, }; ```

      最后,你需要在组件的 mounted 或者 beforeCreate 生命周期钩子中调用这个路由守卫:

      js mounted() { this.$router.beforeEach((to, from, next) => { if (to.meta.defaultPath) { Location.href = to.meta.defaultPath; } else { next(); } }); },

      这样,当路由发生变化时,Vue 就会自动检测并应用新的路由。如果路由未改变,那么旧的路由就会被保持不变。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部