vue.js 组件中引入vuex中不同模块的数据

751 阅读1分钟

一、在不同组件中引入

在组件中引入vuex

import { createNamespacedHelpers } from "vuex";
let { mapState, mapMutations, mapActions } = createNamespacedHelpers("模块名");

在computed中引入数据

comcuputed:{ ...mapState(["数据1", "数据2"]) },

在methods中引入操作

...mapActions(["操作1"]),

二、在相同组件中引入不同模块

在组件中引入vuex

import { mapState, mapMutations, mapActions } from "vuex";

在comcuputed中引入数据

...mapState({
      数据1: state => state.模块名.数据1,
      数据2: state => state.模块名.数据2,
    })
    
    或是
    ...mapState("模块1", ["数据1"]),

在methods中引入操作

...mapActions("模块1", ["操作"]),
...mapActions("模块2", ["操作"]),