vue3 onMounted is called when there is no active component

Vue 版本:3.0.0
出现的问题:

在 setup() 中 onMounted() onActivated(),出现警告:
[Vue warn]: onMounted is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.

[Vue warn]: onActivated is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup(). If you are using async setup(), make sure to register lifecycle hooks before the first await statement.

页面初始加载时,也不执行钩子函数

解决办法:

将 onMounted() onActivated() 代码提前,放在 setup()函数的最前端(可以在 useI18n(), useRoute() 等之后),神奇的好了。

原因: 未知

不知新版本 vue3.2,会不会出现这个问题

posted @ 2022-08-26 10:30  zhanglw  阅读(7139)  评论(0编辑  收藏  举报