关于mutations中只能进行异步操作的传说,接下来实验来 颠覆 一下
store.js
state:{
firstname:'===我在ajax发出之前==='
},
mutations:{
mPost(state){
axios.post('http://localhost:2000/api/user/login',{
username:'linxiao',
password:'654321'
}).then(res=>{
console.log(res)
state.firstname = "==="+res.data.message+'我是mutations==='
})
}
},
actions:{
aPost(state){
axios.post('http://localhost:2000/api/user/login',{
username:'linxiao',
password:'654321'
}).then(res=>{
console.log(res)
state.state.firstname = "==="+res.data.message+'我是ations==='
})
}
}
app.vue
<button @click="mPost">mutations中发axios</button>
<button @click="aPost">ations中发axios</button>
mPost(){
this.$store.commit('mPost')
},
aPost(){
this.$store.dispatch('aPost')
},
页面显示
发送axios之前:
触发mutatios后:
触发atios后: