vue3中使用pinia报错
问题: Uncaught Error: [🍍]: "getActivePinia()" was called but there was no active Pinia. Are you trying to use a store before calling "app.use(pinia)"?
原代码:
const app = createApp(App)
const pinia = createPinia()
const store = useAllDataStore()
app.config.globalProperties.$api = api
app.use(ElementPlus)
store.dynamicRoute(router,"refresh")
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
app.use(pinia)
app.use(router).mount('#app')
我看到pinia也在main.js中挂载了, 以为没问题, 但是f12之后发现问题: Are you trying to use a store before calling "app.use(pinia)"?
调整位置后代码:
const app = createApp(App)
const pinia = createPinia()
app.config.globalProperties.$api = api
app.use(ElementPlus)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
app.use(pinia)
const store = useAllDataStore()
store.dynamicRoute(router, 'refresh')
app.use(router).mount('#app')
即将store的代码都放在app.use(pinia)后