在vue组件中获取未定义的response.data console.log(this.$store.getters.getData)
。
但是,我无法使用此语句console.log(resp.data)
我已经在成功突变中尝试过console.log(data)
,但是它仍然返回未定义状态。
login.vue
login: function() {
let data = this.$store
.dispatch("login", data)
.then((resp) => {
console.log(resp.data); -> it returned data
console.log(this.$store.getters.getData); -> it returned undefined
}
})
.catch(err => {});
}
store.js
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import qs from 'qs'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
status: '',
token: localStorage.getItem('token') || '',
data: {},
},
mutations: {
success(state, token, data) {
state.status = 'success'
state.token = token
Vue.set(state, 'data', data)
}
},
actions: {
login({ commit }, data) {
return new Promise((resolve, reject) => {
axios.post(url, qs.stringify(data))
.then(resp => {
commit('success', resp.data.token, resp.data)
resolve(resp)
})
.catch((error) => {
commit('error')
reject(error)
})
})
},
getters: {
getData: state => state.data,
}
})
console.log(resp.data); -> it returned data
console.log(this.$store.getters.getData); -> it returned undefined
答案 0 :(得分:0)
尝试突变成功,而不是Vue.set(state,'data',data)写state.data = data;
答案 1 :(得分:0)
最后,我弄清楚了为什么它返回未定义的原因,因为该突变只接受2个称为状态和有效载荷的参数,所以其他参数将不被传递。
参考-Can't send in a second parameter to the mutation in Vue store