sunmoon1983
V2EX  ›  问与答

求助:关于 Pinia

  •  
  •   sunmoon1983 · Mar 10, 2022 · 1497 views
    This topic created in 1541 days ago, the information mentioned may be changed or developed.

    有如下代码: 声明了一个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
    }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1088 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 23:39 · PVG 07:39 · LAX 16:39 · JFK 19:39
    ♥ Do have faith in what you're doing.