vue3 ts組合式API異常onMounted is called when there is no active component解決
遇到問題
最近寫vue3+ts和組合式API遇到了上面的問題,代碼如下:
<template> </template> <script setup lang="ts"> import { useStore } from 'vuex' import { useRoute } from 'vue-router' import { onMounted } from 'vue' const store = useStore() store.dispatch('initMenus') const route = useRoute() onMounted(() =>{ console.log(route.path) }) </script> <style lang="scss" scoped> </style>
這個是因為在這個組合式onMounted之前調(diào)用了store.dispatch('initMenus') 內(nèi)部包含async/await
解決方法
If you are using async setup(), make sure to register lifecycle hooks before the first await statement.
只要把代碼順序調(diào)整如下,報錯即可消失:
<template> </template> <script setup lang="ts"> import { useStore } from 'vuex' import { useRoute } from 'vue-router' import { onMounted } from 'vue' const route = useRoute() onMounted(() =>{ console.log(route.path) }) const store = useStore() store.dispatch('initMenus') </script> <style lang="scss" scoped> </style>
希望可以幫到你。
以上就是vue3 ts組合式API異常onMounted is called when there is no active component解決的詳細(xì)內(nèi)容,更多關(guān)于vue3 ts組合式API異常的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue3和Electron實(shí)現(xiàn)桌面端應(yīng)用詳解
本文主要介紹了Vue3和Electron實(shí)現(xiàn)桌面端應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07Vue頁面刷新記住頁面狀態(tài)的實(shí)現(xiàn)
這篇文章主要介紹了Vue頁面刷新記住頁面狀態(tài)的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-12-12解決vuex刷新狀態(tài)初始化的方法實(shí)現(xiàn)
這篇文章主要介紹了解決vuex刷新狀態(tài)初始化的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue自定義組件如何通過v-model指令控制組件的隱藏、顯示
這篇文章主要介紹了vue自定義組件如何通過v-model指令控制組件的隱藏、顯示,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05