vuex仓库请求数据 请求到的数据然后再组件中获取数据
先是在vuex仓库当中写请求数据
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
cartListGet: []
},
mutations: {
GETDATACART(state, pylaod) {
state.cartListGet = pylaod
console.log(state.cartListGet)
}
},
actions: {
getCartList(conText, pylaod) {
axios({
url: 'http://localhost:8081/wx/cart/index',
headers: {
'X-Litemall-Token': localStorage.getItem('X-Litemall-Token')
}
}).then(res => {
if(res.status === 200) {
conText.commit('GETDATACART', res.data.data.cartList)
}
})
}
},
})
请求完之后想要组件中获取数据的时候 这样写
mounted() {
this.$store.dispatch('getCartList')
this.cartGetList = this.$store.state.cartListGet
}