The vuex warehouse requests the requested data and then obtains the data from the component.
First, write the request data in the vuex warehouse
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)
}
})
}
},
})
After requesting, write like this when you want to get data from the component
mounted() {
this.$store.dispatch('getCartList')
this.cartGetList = this.$store.state.cartListGet
}