// store.js
state: {
shopList: []
},
mutations: {
changeShopList (state, arr) {
state.shopList = arr
}
},
actions: {
getShopList ({ commit, state }) {
global.axios.get(url).then(res => {
let { data: { code, data: { list } } } = res
if (+code === 200) {
list.unshift({ id: 0, shop_name: '全部门店' })
commit('changeShopList', list)
}
})
}
}
// index.vue
computed: {
shopList () {
console.log('computed', this.$store.state.shopList) // 空数组
return this.$store.state.shopList
}
},
created () {
this.$store.dispatch('getShopList')
console.log('created', this.shopList) // 空数组
}
现在问题是我需要打印出来请求到的数据而不是一开始定义的空数组,请教大佬怎么解决这个问题?