Vuex操作返回undefined和null

时间:2018-06-01 08:20:08

标签: vue.js login axios vuex

嘿伙计我正在使用Vue,Vuex和Axios创建登录。但是,当我按下登录按钮而不是打印用户名时,它只打印访问令牌。此外,当我检查devtools时,它显示某些对象/变量未定义。我不确定为什么会发生这种情况,希望你能提供帮助,因为我们将非常感激。

index.js(Vuex):

import vuex from 'vuex';
import axios from 'axios';
import mutations from './mutations';


const createStore = () => {
  return new vuex.Store({
    state: {
      accessToken: "Hello",
      user: {
          dob: null,
          email: null,
          firstName: null,
          gender: null,
          id: null,
          lastName: null,
          profileImage: null,
          registered: null,
          verified: null
      }
    },
    getters: {
      accessToken: state => {
        return state.accessToken;
      },
    },
    actions: {
      init(store) {
        store.actions.setToken(store, JSON.parse(localStorage.getItem('accessToken') || ''));
      },

      setToken(store, value) {
        store.commit('SET_TOKEN', value);
      },

      login(store, value){
          store.commit('SET_TOKEN', value);
          store.dispatch('pullUserDetails');
      },

      pullUserDetails(store){
        console.log(store);
        axios.get(
            'https://api.ticketpass.co/user',
            {
                headers: {
                    'Authorization' : 'Bearer' + store.state.accessToken
                }
            }
        )
        .then((response) => {
            console.log(response.data);
            store.state.user.dob = response.data.dob;
            store.state.user.email = response.data.email;
            store.state.user.firstName = response.data.firstName;
            store.state.user.gender = response.data.gender;
            store.state.user.id = response.data.id;
            store.state.user.lastName = response.data.lastName;
            store.state.user.profileImage = response.data.profileImage;
            store.state.user.registered = response.data.registered;
            store.state.user.verified = response.data.verified;
        },
        (error) => {
            error.response.id;
            error.response.firstName;
            error.response.lastName;
            error.response.email;
            error.response.profileImage;
            error.response.dob;
            error.response.gender;
            error.response.registered;
            error.response.verified;
        }
      );
    }
    },
    mutations
  });
};

export default createStore;

mutations.js

import { error } from "util";

const mutations = {
  setToken(state, token) {
    state.accessToken = token;
  },

  SET_TOKEN(state, value) {
    console.log(value);
    localStorage.setItem('accessToken', value);
    state.accessToken = value;
    console.log(state.accessToken);
  },


};

export default mutations;

1 个答案:

答案 0 :(得分:1)

您在操作中获得的第一个参数是context,它与store对象具有相同的属性。

在您正在进行的init行动中

init(store) {
   store.actions.setToken(store, JSON.parse(localStorage.getItem('accessToken') || ''));
 }

您尝试发送setToken行动。

但是context参数没有actions属性。

要发送另一个操作,您应使用dispatch参数为您提供的context方法。

所以在你的init行动中

init(store) {
    store.dispatch('setToken', JSON.parse(localStorage.getItem('accessToken') || ''));
  },

参考 - vuex actions