美文网首页
vuex学习笔记-actions

vuex学习笔记-actions

作者: 塔塔七 | 来源:发表于2020-04-08 12:18 被阅读0次

actions(异步操作更改数据需要在actions中进行,如果不使用,devtools将不显示数据,不便于调试)

.vue中写法
this.$store.dispatch('updateInfo','我是payload')
.then((res)=>{
  console.log(res)
})

actions.js
actions: {
    updateInfo(context,payload){
        return new Promise((resolve,erject)=>{
           setTimeout(()=>{
               context.commit('update')
                console.log(payload)
                resolve('数据已完成修改')
            },1000)
        })
     
  }
}
mutations.js
mutations: {
    update(state){
       state.count  =  2
  }
}
store中index.js
state: {
   count  =  1
}

相关文章

网友评论

      本文标题:vuex学习笔记-actions

      本文链接:https://www.haomeiwen.com/subject/udrzphtx.html