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

2021-08-05 16:31:24 +08:00
 zzlit

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.

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

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

学会搜 warning
如果没有就下载 vue3/vue-router 源码搜 warning,里面都能知道什么情况会出这个 warning
zzlit
2021-08-06 16:08:17 +08:00
@shakukansp 受教了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/793881

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX