一、报错信息:
该错误由于动态创建节点并调用包含Pinia的组件引起。通过分析,发现问题在于组件外部创建时,Pinia的setup环境缺失。
解决方案是确保导出并使用export function来在setup之外的地方访问Pinia。总结来说,当在Pinia中进行函数式调用并动态创建节点时,需导出Pinia相关模块以避免警告。
二、问题解决
改成以下方式:
1、先引入 import store from '../index.js'
2、然后定义常量 useStore,
3、最后 导出返回包含 useStore(store) 的函数。
完整示例:
import Cookies from 'js-cookie'
import store from '../index.js'
const useStore = defineStore(
'app',
{
state: () => ({
sidebar: {
...
},
...
}),
actions: {
...
}
})
export default function useAppStore() {
return useStore(store)
}
store/index.js文件
import { createPinia } from "pinia";
const store = createPinia();
export { store };