场景
在封装的HTTP中使用pinia,如下:
结果报错:
Uncaught Error: [🍍]: "getActivePinia()" was called but there was no active Pinia. Are you trying to use a store before calling "app.use(pinia)“
报错原因:
在使用pinia时,还没有在main.js中完成注册
pinia文档
解决方法
1、在stores中新建index.js
import { createPinia } from 'pinia'
const pinia = createPinia()
export default pinia
2 、在main.js中引入并注册
import pinia from './stores'
app.use(pinia)
3、在http.js中引入并传入
import pinia from '@/stores/index.js'
import { useCommonStore } from '@/stores/common'
const commonState = useCommonStore(pinia)
完毕