有如下代码:
声明了一个app store
import { getAppAll } from '@/api/app';
import { defineStore } from 'pinia';
import { IAppInfo } from '@/utils/types';
import { store } from '@/store';
export const useAppStore = defineStore('app', {
state: () => ({
appList: [],
}),
getters: {
appList: (state) => {
console.log('getter', state.appList);
return state.appList;
},
},
actions: {
async fetchAppList() {
const resp = await getAppAll();
const { data } = resp;
const result = data as Array<IAppInfo>;
this.$patch((state) => {
state.appList = result;
});
},
},
});
export function getAppStore() {
return useAppStore(store);
}
为什么我在其它 组件中获取不到 appList 的值呢?一直是 undefind 其它 组件调用的代码:
import { getAppStore } from '@/store/modules/app';
const appStore = getAppStore();
const { appList } = appStore;
console.log('appList', appList); === undefind
const apps = ref(appList);
if (helper.len(apps.value) === 0) {
await appStore.fetchAppList();
apps.value = appStore.appList;
console.log('apps.value', appStore.appList); === undefind
}