V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
sunmoon1983
V2EX  ›  问与答

求助:关于 Pinia

  •  
  •   sunmoon1983 · 2022-03-10 13:15:43 +08:00 · 957 次点击
    这是一个创建于 1023 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有如下代码: 声明了一个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
    }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2722 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 12:19 · PVG 20:19 · LAX 04:19 · JFK 07:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.