第一步 先在项目中安装axios
npm i axios
然后在项目中引用axios
import axios from 'axios'
axios 的get方法 post方法 以及传入参数
import axios from 'axios'
export default {
state: {
updateCartList: []
},
actions: {
updateCartList ({ commit }) {
axios.get('/api/cart').then(response => {
commit('UPTATE_CAR_LIST', response.data)
})
},
addCart ({ commit }, productlistIteam) {
axios.post('/api/cart', productlistIteam).then(response => {
commit('UPTATE_CAR_LIST', response.data)
})
},
rmCart ({ commit }, rmId) {
axios.delete('/api/cart/delete', { params: { id: rmId } }).then(response => {
commit('UPTATE_CAR_LIST', response.data)
})
}
},
mutations: {
UPTATE_CAR_LIST (state, data) {
state.updateCartList = data
}
},
getters: {
cartQuantity (state) {
return state.updateCartList.reduce((Quantity, ele) => Quantity + ele.quantity, 0)
},
cartTotal (state) {
return state.updateCartList.reduce((total, ele) => total + ele.quantity * ele.price, 0)
}
}
}