pinia如果不在组件中使用会报错
pinia.mjs:1709 Uncaught Error: [🍍]: "getActivePinia()" was called but there was no active Pinia. Are you trying to use a store before calling "app.use(pinia)"?
See https://pinia.vuejs.org/core-concepts/outside-component-usage.html for help.
This will fail in production.
at useStore (pinia.mjs:1709:19)
at default.global.ts:2:15
这时候看Nuxt或者别人的csdn是这样写的
import useBreadcrumbStore from '@/store/breadcrumbStore'
import { createPinia } from "pinia"
const store = useBreadcrumbStore(createPinia())
export default defineNuxtRouteMiddleware((to, from) => {
console.log(store.navigationObj)
})
虽然可以解决报错,但是出来之后在ts中并不能使用在组件中传给store的数据
这个问题困扰了我整整两天半!!!
最后的解决方法非常简单
import useBreadcrumbStore from '@/store/breadcrumbStore'
export default defineNuxtRouteMiddleware((to, from) => {
const store = useBreadcrumbStore()
console.log(store.navigationObj)
})
只需要在导出之内使用就可以了
同样在使用axios的时候,想要在拦截前或者拦截后存储一些数据,这个方法同样适用!!!