Vue如何获取和使用Vuex

245 阅读1分钟

1.第一种方法

//在组件中通过import 引入vuex 拿到createNamespacedHelpers
        import {createNamespacedHelpers} from "vuex"
        const {mapState,mapMutations,mapActions}  = createNamespacedHelpers("模块化的名字")
//使用
在computed中通过下面方式使用
...mapState(['使用的变量'])
在methods中使用
...mapMutations([''])
...mapActions([''])

2.第二种方式

import { mapState, mapMutations } from 'vuex';

//使用
在computed中通过下面方式使用
...mapState('users', ['curUser']), //users是模块化的名字   //curUser变量
在methods中使用
 ...mapMutations('users', ['UPDATE_USERINFO']),