pinia的坑getActivePinia was called with no active Pinia. Did you forget to install

516 阅读1分钟

本地开发是没有问题的,但是一打包就提示这个报错

# getActivePinia was called with no active Pinia. Did you forget to install pinia?

gpt的回答

image.png

显然不是这个原因引起的,因为之前的pinia也是可以使用的

查看了git提交的代码,发现自己封装的组件在import一个全局的方法,vue3由于取消了过滤器,我自己就写了一个全局的方法进行过滤,进行中英文的切换

由于这个全局方法进行中英文切换需要使用到pinia,正常使用是没有问题的,但是我在组件进行导入后再进行引入pinia就出现打包后的问题

import { useAppStore } from '@/store/modules/app';

import { ref } from 'vue';

const appStore = useAppStore();//这里使用了pinia


export function filters(id: number, arr: any) {

  console.log('id', id,arr)

  if (!id) return

  const res: any = arr.find((item: any) => item.id === id);

  if (res && res.name) {

    return res.name;

  } else if (res) {

    return appStore.locale.name == 'zh-cn' ? res.cn : res.en;

  }

}
<template>
  ...
</template>

<script lang='ts' setup>

import { useAppStore } from '@/store/modules/app';
import { filters } from '@/utils/filter';//这里导入了全局过滤器
const appStore = useAppStore();//再次导入pinia
....

</script>
<style lang='scss' scoped>
</style>

解决方案,直接使用全局的过滤导入,不要使用局部导入

import { getCurrentInstance } from 'vue' 
const { dateFormat } = getCurrentInstance().appContext.config.globalProperties.$filters