首页 > 其他分享 >vue3在单独的js文件中使用pinia报错:getActivePinia was called with no active Pinia. Did you forget to install pin

vue3在单独的js文件中使用pinia报错:getActivePinia was called with no active Pinia. Did you forget to install pin

时间:2022-09-20 18:34:29浏览次数:94  
标签:文件 createPinia getActivePinia js 报错 pinia import

1、之前在main.js中使用方式是:
import { createPinia } from 'pinia'
const pinia = createPinia();
app.use(pinia);
2、现在的问题是我要再建一个js文件,需要用到我建的pinia的store,然后报错:getActivePinia was called with no active Pinia. Did you forget to install pinia?
问题就是要重新在这个js文件中注册一下pinia
解决办法,为pinia单独新建一个文件store.js 内容也很简单,就三行
import { createPinia } from 'pinia'; const pinia = createPinia(); export default pinia;
3、然后在要使用pinia的js文件中导入,也很简单
import pinia from '@/stores/store' import { useSystemDataStore } from "@/stores/index"; // SystemDataStore 可以在本文件中随意使用 const SystemDataStore = useSystemDataStore(pinia);
然后就可以使用 SystemDataStore.apiDomain 或者 useSystemDataStore().apiDomain 都可以,因为已经注册过了pinia

标签:文件,createPinia,getActivePinia,js,报错,pinia,import
From: https://www.cnblogs.com/xsj1989/p/16712066.html

相关文章