注册了pinia还是报错:“getActivePinia()“ but there was no active Pinia. Did you forget to install pinia?

所遇问题:“getActivePinia()” was called but there was no active Pinia. Did you forget to install pinia?
const pinia = createPinia()
app.use(pinia)

在路由鉴权(permission)文件中想使用 pinia 限制 token

pinia 已经在main中注册了 也将所有的 pinia 信息都放置在最顶层 但还是报以上错误,官方文档并没讲解这块, 排查许久后, 最终发现:

import { createPinia } from 'pinia';
import { useSystemInfoObj } from '@/stores/modules/systemInfoObj.js';
const pinia = createPinia();
const systemInfoObjStore = useSystemInfoObj(pinia); // 将实例传入

至于/stores/modules/systemInfoObj嘛:

export const useSystemInfoObj = defineStore('initSystemInfo', {
  state: () => ({
    device: '',
  }),
  actions: {
  },

  getters: {
    getInitSystemInfo: (state) => {
    }, 
  },

  modules: {},
});

也就是这句: const appStore = useAppStore(pinia), 需要在useAppStore中直接加入 pinia 实例参数完成注册(why? 有人知道吗?)。
感谢原博主,因为官方文档确实没有解释这一块

你可能感兴趣的:(前端,服务器,javascript)