get_pins get_ports(pinia报错:getActivePinia was called with no active Pinia. Did you forget to install pinia?)
导读:项目场景: 项目:vue3+pinia+vite+element-plus...
项目场景:
项目:vue3+pinia+vite+element-plus
问题描述
pinia.mjs:1692 Uncaught Error: [🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?
错误:在没有激活Pinia的情况下调用getActivePinia 。
报错代码:login.ts文件下
import { useUserStore } from ../../store/user import { useRoute } from vue-router const user = useUserStore()报错位置:login.ts文件执行位置:
layout组件中 。原因分析:
报错:在没有激活Pinia的情况下调用getActivePinia
分析:
login.ts中应用了pinia状态管理 main.ts文件中尚未app.use(store) ,已经在layout文件中运行了login.ts文件这里的问题像是异步导致的(不确定) ,main.ts中app.use(store).use(router).mount(“#app ”) ,先挂store再挂router也不行 ,所以猜测是异步。
解决方案:
import store from @/store/index.ts import { useUserStore } from ../../store/user import { useRoute } from vue-router const user = useUserStore(store)以上是vue3中pinia的用法 ,在需要的页面再次引入pinia实例store即可 。不同的是 ,其他页面引入 ,只需用const user = useUserStore() ,激活pinia前引入需要传入pinia实例:const user = useUserStore(store) 。
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!