vuex模块化的写法及用法:state、getteer、mutations、actions


一、main.js:挂载

import store from './store/index';
Vue.prototype.$store = store;

二、index.js:导出 store

import Vue from "vue";
import Vuex from "vuex";
import app from "./modules/app";

Vue.use(Vuex);
const store = new Vuex.Store({
  modules: {
    app,
  },
});
export default store;

三、app.js:导出app模块

/**
 * @example
 * 1、state
 * this.$store.state.app.user.name
 *
 * 2、mutations
 * this.$store.commit("app/setUser", { name: "熊熊", age: 12, sex: "男" });
 *
 * 3、actions
 * (1) 以载荷形式访问
 * this.$store.dispatch("app/updateUser",{ name: "嘻嘻", age: 11, sex: "女" });
 * (2) 以对象形式访问
 * this.$store.dispatch({
 *  type: "app/updateUser",
 *   user: { name: "憨憨", age: 11, sex: "女" },
 * });
 *
 * 4、getters
 * (1) 不带参数
 * this.$store.getters["app/getAll"]
 * (2) 带参数
 * this.$store.getters["app/getByIndex"](0);
 */
const app = {
  state: {
    user: {
      name: "小明",
      age: "20",
      sex: "男",
    },
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
    },
  },
  actions: {
    updateUser(context, obj) {
      // 以载荷形式
      context.commit("setUser", obj);
      // 以对象形式
      // context.commit("setUser", obj.user);
    },
  },
  getters: {
    // 通过属性访问
    getName(state) {
      return "名字:" + state.user.name;
    },
    getAge(state) {
      return "年龄:" + state.user.age;
    },
    getsex(state) {
      return "性别" + state.user.sex;
    },
    getAll(state) {
      const { name, age, sex } = state.user;
      return "名字:" + name + "\n" + "年龄:" + age + "\n" + "性别" + sex;
    },
    // 通过方法访问
    getByIndex: (state) => (index) => {
      const { name, age, sex } = state.user;
      switch (parseInt(index)) {
        case 0:
          return "名字:" + name;
        case 1:
          return "年龄:" + age;
        case 2:
          return "性别" + sex;
        case 3:
          return "名字:" + name + "\n" + "年龄:" + age + "\n" + "性别" + sex;
        default:
          return "";
      }
    },
  },
  //使其成为带命名空间的模块。保证在变量名一样的时候,添加一个父级名拼接。
  namespaced: true,
};

export default app;

四、结构

在这里插入图片描述

五、具体使用

1、state

(1) 方式一

// this.$store.state.模块名称.模块属性
this.$store.state.app.user.name

(2) 方式二:辅助函数mapState

// 引用user:this.user
import { mapState } from 'vuex'
computed: {
    ...mapState({
      user: (state) => state.模块名称.模块属性
    }),
  },

2、getters

(1)方式一

/*不带参数
* this.$store.getters['模块名/xxx']
* 带参数
* this.$store.getters['模块名/xxx']()
*/ 
this.$store.getters["app/getAll"]
this.$store.getters["app/getByIndex"](3)

(2)方式二:辅助函数mapGetters

// 引用userName:this.userName
import { mapGetters} from "vuex";
computed: {
	...mapGetters({
      userName: "模块名/xxx",
    }),
},

3、mutations

(1) 方式一

/*
* 1、mutation的事件类型:每个mutation都有一个字符串的事件类型(type)和一个回调函数(handler)。
* 在这个代码中,setUser就是mutation的一个事件类型。
* 2、载荷(payload):指通过store.commit传入的额外参数。大多数情况下,载荷是一个对象
*
* this.$store.commit("模块名/mutations事件类型", 载荷)
*/ 
this.$store.commit("app/setUser", { name: "熊熊", age: 12, sex: "男" })

(2)方式二:辅助函数mapMutations

import {  mapMutations } from "vuex";
methods: {
  ...mapMutations({
    setUser: "模块名/mutations事件类型",
  }),
  example() {
    this.setUser({ name: "熊熊", age: 12, sex: "男" });
  },
},

4、actions

(1) 方式一

/*
* 1、以载荷形式分发
* this.$store.dispatch('模块名/方法',传参)
* 
* 2、以对象形式分发
* this.$store.dispatch({
* 	type:'模块名/方法',
* 	传参
* })
*/ 
this.$store.dispatch("app/updateUser",{ name: "嘻嘻", age: 11, sex: "女" });
this.$store.dispatch({
     type: "app/updateUser",
     user: { name: "憨憨", age: 11, sex: "女" },
});

(2) 方式二:辅助函数mapActions

import { mapActions } from "vuex";
methods: {
   ...mapActions({ updateUser: "模块名/方法" }),
   example() {
     this.updateUser({ name: "熊熊", age: 12, sex: "男" });
   },
 },
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值