store里请求数据
import Axios from 'axios'
export default new Vuex.Store({
// 共享数据
state: {
carlists: [],
},
mutations: {
getdata(state, res) {
state.carlists = res
},
},
actions: {
getdata(context) {
Axios.get('http://127.0.0.1:3007/api/cartlist')
.then((res) => {context.commit('getdata', res.data.result)
})
}
}
}
组件里可得到 打印数据 在查看state里数据,已经传过来了
mounted(){
this.$store.dispatch('getdata')
}
现在就可以使用vuex里的carlists了