Vue.js VUE:登录后更新存储数据

Vue.js VUE:登录后更新存储数据,vue.js,vuejs2,vue-component,vue-router,vuex,Vue.js,Vuejs2,Vue Component,Vue Router,Vuex,我有一个问题,商店数据“菜单”在我登录后没有更新。 表面上。。在我调用“getMenus”之前,对象“loggedInUser”不是sat。。我不确定我做错了什么 PS!在chrome中调试时,我注意到loggedInUser在输入api调用时为“null”(请参阅api.js代码片段) Login.vue(方法): methods: { doLogin() { this.errorMessage = ''; this.loading = true;

我有一个问题,商店数据“菜单”在我登录后没有更新。 表面上。。在我调用“getMenus”之前,对象“loggedInUser”不是sat。。我不确定我做错了什么

PS!在chrome中调试时,我注意到loggedInUser在输入api调用时为“null”(请参阅api.js代码片段)

Login.vue(方法):

methods: {
    doLogin() {
        this.errorMessage = '';
        this.loading = true;
        let userCredentials = {
            'username': this.loginEmail,
            'password': this.loginPassword
        };

        this.$store.dispatch('tryLogin', {
            'login': this.loginEmail,
            'password': this.loginPassword
        }).then((response) => { 
            this.$store.dispatch('getMenus')
                .then((response) => {
                    this.$router.push('/')
            });
        });
    }
},

Menus.vue(与/)相同

store.js(获取菜单操作和tryLogin)

api.js(获取菜单功能)


从您的
store.js
代码片段中,您似乎忘记了返回承诺。

ie,
return api.tryLogin(凭证)…
。看
 computed: {
    menus() {
        return this.$store.getters.menus
    }
    },
    created() {
        this.$store.dispatch('getMenus')
    },
methods: {
    viewMenu: function(item) {
        console.log("=> View Menu : " + item.Name)
        this.$router.push('/viewmenu/' + item.Id)
    }
    }
}
actions: {
    getMenus({ commit, getters }) {
        api.getMenus(getters.loggedInUser)
            .then(menus => {
                commit('UPDATE_MENUS', menus);
            });
    },
    tryLogin({ commit }, credentials) {
        api.tryLogin(credentials)
        .then(loggedInUser => {
            commit('LOGGED_IN_USER', loggedInUser);
        });
    },
getMenus(loggedInUser) {
    var hostname = 'http://myurl'

    var config = {
        headers: {
            'Content-Type': 'application/json'
        }
    }
    var endpointUrl = hostname + '/api/Menu/GetMenus';

    if (loggedInUser != null){
        endpointUrl = hostname + '/api/Menu/GetMenusForSubCompany/' + loggedInUser.encryptedsubcompanyid;
    }

    return axios.get(endpointUrl, config)
        .then(response => response.data);
},