gpt4 book ai didi

javascript - Vuex getter 返回未定义

转载 作者:行者123 更新时间:2023-12-01 15:11:25 25 4
gpt4 key购买 nike

我有一个使用 vex 存储站点范围数据的 vue 应用程序,我获取用户信息并将其置于创建生命周期方法之前的状态,如下所示:

const app = new Vue({
el: '#app',
store,
beforeCreate(){
store.dispatch('currentUser');
}
});

在我的用户模块中,我有一个获取用户名称的 getter,如下所示:
const getters = {
getName(state){
return state.user.name;
}
};

在我的组件中,我试图访问 getter 以在组件中显示用户的名称。当我查看 Vue Chrome 开发工具时,getter 不是未定义的,但是当我控制台记录 getter 结果时,它显示为未定义。
mounted(){
console.log(store.getters.getName);
},

似乎组件在加载状态之前正在加载。我正在使用 axios 获取用户信息。我真的很困惑如何解决这个问题,因为我无法尽快调用该操作来获取用户数据。

如果在我的 getter 方法中我这样做:
state.user

我明白了:
{ "id": 1, "name": "Test" }

但是当我尝试获取名称时,我得到了未定义

这里是商店:
import axios from "axios/index";

const state = {
user: {},
};

const mutations = {
FETCH_USER(state,user){
state.user = user;
},
UPDATE_AVATAR(state,avatar){
state.user.avatar = avatar;
}
};

const getters = {
getName(state){
return state.user.name
},
avatar(state){
return state.user.avatar;
},
userSocialNetworks(state){
//return state.user.social_networks
},
schoolName(state){
return state.user.school
},
schoolAbout(state){
return state.user.school.about
},
schoolAddress(state){
return state.user.school.address
}
};

const actions = {
currentUser: ({commit}) => {
axios.get('/api/user').then(response => {
commit('FETCH_USER', response.data);
});

}
}

export default {
state,
getters,
actions,
mutations

}


{"user":{"user":{"name":"Test","email":"test@test.edu","avatar":"http://www.gravatar.com/avatar/xxxx=300","city":null,"state":null,"zip":null,"address":null,"lat":null,"long":null,"role":"school","school":{"id":1,"about":null,"header":null,"name":"Test","user_id":1,"created_at":"2018-06-06 19:48:16","updated_at":"2018-06-06 19:48:16"},"following":[],"followers":[],"social_networks":[{"id":4,"user_id":1,"social_network_id":1,"network_url":"test.com/k","created_at":"2018-06-06 23:11:09","updated_at":"2018-06-06 23:15:19"},{"id":5,"user_id":1,"social_network_id":2,"network_url":"test.com/k","created_at":"2018-06-06 23:15:19","updated_at":"2018-06-06 23:15:19"},{"id":6,"user_id":1,"social_network_id":5,"network_url":"test.com/k","created_at":"2018-06-06 23:16:15","updated_at":"2018-06-06 23:16:15"}]}},"socialNetowrks":{"available_networks":[{"id":1,"network_name":"Facebook","created_at":null,"updated_at":null},{"id":2,"network_name":"Instagram","created_at":null,"updated_at":null},{"id":5,"network_name":"Twitter","created_at":null,"updated_at":null}]}}

最佳答案

您在这里面临两个大问题:

  • 您不应该直接在 vuex 存储中更新对象,否则您将失去 react 性

  • 因此,请确保正确声明所有状态并分配给对象而不是覆盖它们
    import assignDeep from 'assign-deep'; //Since you have nested structure you will need this

    const state = {
    user: {
    name: '',
    school: {
    about: '',
    address: '',
    },
    // Make sure all properties are defined
    },

    };

    const mutations = {
    FETCH_USER(state,user){
    assignDeep(state.user, user);
    },
    UPDATE_AVATAR(state,avatar){
    state.user.avatar = avatar;
    }
    };
  • 您正在尝试访问在状态上异步定义的属性并同步使用它(在它存在之前)

  • 首先使用 mapGetters vuex的实用程序,更容易使用
    import { mapGetters } from 'vuex';
    //... In component definition
    computed: {
    ...mapGetters({name: 'getName'})
    }
    由于这是一个 vue 计算属性,因此您的组件将在定义时重新渲染,并且所有内容都应该正确显示您只需要确保在它为空时处理这种情况,并且可能会显示加载屏幕直到它
    例如
    <template>
    <div v-if="!name" class="loading"></div>
    <div v-else>{{name}}</div>
    <template>
    您也可以自由地等待属性更新
    不要在挂载的钩子(Hook)中使用它,因为它仍然是那里的默认值
    而是使用 watcher or a computed property取决于你的需要
    watch: {
    name: {
    handler(name, oldName) {
    if (name) {
    //Do something
    }
    },
    immediate: true
    }
    }

    关于javascript - Vuex getter 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731429/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com