Pinia在Nuxt3中使用,不在setup()中使用,Pinia报错的解决方法

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的时候,想要在拦截前或者拦截后存储一些数据,这个方法同样适用!!!

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值