V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zzlit
V2EX  ›  Vue.js

求教 vue3 中使用 vue-router 的导航守卫问题

  •  
  •   zzlit · 2021-08-05 16:31:24 +08:00 · 1840 次点击
    这是一个创建于 985 天前的主题,其中的信息可能已经有所发展或是发生改变。

    app.vue 代码如下

    <template>
      <router-view />
    </template>
    
    <script lang="ts">
    import { defineComponent } from 'vue';
    import { onBeforeRouteLeave } from 'vue-router';
    
    export default defineComponent({
      name: 'App',
      setup() {
        onBeforeRouteLeave((to, from) => {
          console.log('onBeforeRouteLeave', to, from);
        });
      }
    });
    </script>
    

    跑起来控制台有警告,同时也不会触发 onBeforeRouteLeave 这个事件

    [Vue Router warn]: No active route record was found. Are you missing a <router-view> component?

    然后放到页面内能触发,但是同样也会有警告:

    [Vue warn]: Avoid app logic that relies on enumerating keys on a component instance. The keys will be empty in production mode to avoid performance overhead.

    请问大佬们这该怎么解决?

    3 条回复    2021-08-06 16:08:17 +08:00
    lupkcd
        1
    lupkcd  
       2021-08-05 23:45:50 +08:00
    应该写在 <router-view /> 里面的组件才行吧,没试验,盲猜的
    shakukansp
        2
    shakukansp  
       2021-08-06 10:38:46 +08:00   ❤️ 1
    https://stackoverflow.com/questions/68100114/avoid-app-logic-that-relies-on-enumerating-keys-on-a-component-instance

    学会搜 warning
    如果没有就下载 vue3/vue-router 源码搜 warning,里面都能知道什么情况会出这个 warning
    zzlit
        3
    zzlit  
    OP
       2021-08-06 16:08:17 +08:00
    @shakukansp 受教了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:20 · PVG 07:20 · LAX 16:20 · JFK 19:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.