VMx

vmx.im
啥都学一点的填坑人
  1. 首页
  2. vue
  3. 正文

pinia在封装请求的文件中使用报错的问题

2022年4月18日 1016点热度 2人点赞 0条评论

在 request 的 请求拦截器 中使用 store却报错:

Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?
    const pinia = createPinia()
    app.use(pinia)
This will fail in production.

查找官网,其实只要把这个 sotre 的申明放到守卫里面去就可以了。这样就不会出现没有初始化 pinia就调用的问题。

import { createRouter } from 'vue-router'
const router = createRouter({
  // ...
})

// ❌ Depending on the order of imports this will fail
const store = useStore()

router.beforeEach((to, from, next) => {
  // we wanted to use the store here
  if (store.isLoggedIn) next()
  else next('/login')
})

router.beforeEach((to) => {
  // ✅ This will work because the router starts its navigation after
  // the router is installed and pinia will be installed too
  const store = useStore()

  if (to.meta.requiresAuth && !store.isLoggedIn) return '/login'
})
Post Views: 399
标签: 暂无
最后更新:2022年4月18日

huifei

这个人很懒,什么都没留下

点赞
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 vmx.im. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang