• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

mandyGuan12

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

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)后

posted on 2024-09-03 13:53  番茄仔Mandy  阅读(1114)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3