vue3中store分模块时其他vue页面中如何使用vuex中的actions mutations states

参考自:https://blog.csdn.net/qq_47452289/article/details/111944935

 

store/account.js

const state={
    userName: null
}

const actions = {
    login({commit}, userinfo){
        return new promise( (resolve, reject) => {
             loginByUserName(userinfo.userName, 
                        userinfo.password).then(res => {
             })

        } )
    
    }  
}

const mutations = {
    setAccount ( state, data ) {
        state.username = data.username
    }
}


export default {
   state,
   actions,
   mutations,
   namespaced: true
}

vue文件中使用

import { mapActions} from "vuex"

methods: {
      ...mapActions("account",["login"]), //映射account模块中的login方法
      
      submit(){
          this.login(userInfo)
      }
}

 

posted @ 2021-05-10 14:47  氧化成风  阅读(705)  评论(0编辑  收藏  举报