美文网首页
2022-03-22-🦜🦜Vuex Action异步执行使用场景

2022-03-22-🦜🦜Vuex Action异步执行使用场景

作者: 沐深 | 来源:发表于2022-03-28 22:14 被阅读0次

vuex action 是全局事件触发的执行函数,
定义

const actions  = {
  login(state) {
    axios('login').then(res ={
      state.login = true
    })
  }
}

// 执行
this.dispatch("login").then(res => {
  
})

相交于一般 muitition具有异步执行的功能😺,
具体是包了一层Promise.resolve,其实完全可以用muitition,commit实现,只要return new Promise

const mutations = {
  login (state) {
    return new Promise(resolve => {
      axios('login').then(res ={
        resolve(res)
        state.login = true
      })
    })
  }
 }
// 执行
this.$store.commit("login").then(res => {})

相关文章

网友评论

      本文标题:2022-03-22-🦜🦜Vuex Action异步执行使用场景

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