Vue2.x中Vuex的多种写法

  1. 首先安装Vuex
yarn add vuex
  1. 在main中引入并且在新建的store文件夹下的index.js文件中使用use注册,至此所有VueComponent、Vue实例中都可以看见$store
main.js
import Vue from "vue";
import App from "./App.vue";
import store from "@/store";
Vue.config.productionTip = false;
new Vue({
  store,
  render: (h) => h(App),
}).$mount("#app");

index.js
import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);
const state = {};
const actions = {};
const getters = {};
const mutations = {};
export default new Vuex.Store({
  state,
  actions,
  getters,
  mutations,
});

  1. 此处我们采用最简单的加减来演示不同的写法,不记录$sotre.的写法
store-->index.js
import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);
const state = {
  count: 0,
};
const actions = {
  addCount(context, value) {
    context.commit("ADD_COUNT", value);
  },
};
const getters = {
  expandCount(state) {
    return state.count * 10;
  },
};
const mutations = {
  ["ADD_COUNT"](state, value) {
    state.count += value;
  },
};
export default new Vuex.Store({
  state,
  actions,
  getters,
  mutations,
});

mapState相当于

storeCount(){
	return this.$store.state.count
}
  • 第一种对象写法 key value
<template>
  <div>
    <div>
      <span>{{ storeCount }}</span>
    </div>
    <button @click="onAddCount">点我加1</button>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    //第一种对象写法 key value
    ...mapState({ storeCount: "count" }),
  },
  methods: {
    onAddCount() {
      this.$store.dispatch("addCount", 1);
    },
  },
};
</script>
  • 第二种数组写法(用于同名)
<template>
  <div>
    <div>
      <span>{{ count }}</span>
    </div>
    <button @click="onAddCount">点我加1</button>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    //第二种数组写法
    ...mapState(["count"]),
  },
  methods: {
    onAddCount() {
      this.$store.dispatch("addCount", 1);
    },
  },
};
</script>

mapGetters使用方法等同mapState

mapActions使用方法同mapMutations,此处以mapMutations为例

  • 第一种对象写法
<template>
  <div>
    <div>
      <span>{{ count }}</span>
      <span>{{ expandCount }}</span>
    </div>
    <button @click="onAddCount(1)">点我加1</button>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["expandCount"]),
  },
  methods: {
    //基础写法 onAddCount不必在template中传参,
    // onAddCount() {
    //   this.$store.commit("ADD_COUNT", 1);
    // },

    // mapMutations相当于 这就导致要在template中点击按钮的时候传入参数
    // onAddCount(value) {
    //   this.$store.commit("ADD_COUNT", value);
    // },

    // 对象写法
    ...mapMutations({ onAddCount: "ADD_COUNT" }),
  },
};
</script>
  • 第二种数组写法
<template>
  <div>
    <div>
      <span>{{ count }}</span>
      <span>{{ expandCount }}</span>
    </div>
    <button @click="ADD_COUNT(1)">点我加1</button>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["expandCount"]),
  },
  methods: {
    //基础写法 onAddCount不必在template中传参,
    // onAddCount() {
    //   this.$store.commit("ADD_COUNT", 1);
    // },

    // mapMutations相当于 这就导致要在template中点击按钮的时候传入参数
    // onAddCount(value) {
    //   this.$store.commit("ADD_COUNT", value);
    // },

    // 对象写法
    // ...mapMutations({ onAddCount: "ADD_COUNT" }),
    //数组写法
    ...mapMutations(["ADD_COUNT"]),
  },
};
</script>

Vuex模块化
重构store中index.js代码,此处为了简便,不单独拆分文件

import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);

const addOptions = {
  //开启命名空间,因为getters actions不区分模块
  namespaced: true,
  state: {
    count: 0,
  },
  actions: {
    addCount(context, value) {
      context.commit("ADD_COUNT", value);
    },
  },
  getters: {
    expandCount(state) {
      return state.count * 10;
    },
  },
  mutations: {
    ["ADD_COUNT"](state, value) {
      state.count += value;
    },
  },
};

const subOptions = {
  namespaced: true,
  state: {
    count: 0,
  },
  actions: {
    subCount(context, value) {
      context.commit("SUB_COUNT", value);
    },
  },
  getters: {
    expandCount(state) {
      return state.count * 10;
    },
  },
  mutations: {
    ["SUB_COUNT"](state, value) {
      state.count -= value;
    },
  },
};

export default new Vuex.Store({
  modules: {
    customAdd: addOptions,
    customSub: subOptions,
  },
});
  • mapState基础实现
<template>
  <div>
    <div>
      <span>{{ customAdd.count }}</span>
    </div>
    <!-- <button @click="onAddCount">点我加1</button> -->
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
  	//取自store下index.js中modules的key
    ...mapState(["customAdd"]),
  },
  methods: {
    // onAddCount() {
    //   this.$store.dispatch("addCount", 1);
    // },
  },
};
</script>

展开$store看下现在的格式,所以要在template中采用customAdd.count去取值
在这里插入图片描述

  • 进阶写法

这种写法必须在store下index.js开启namespaced:true才可以生效

<template>
  <div>
    <div>
      <span>{{ count }}</span>
    </div>
    <!-- <button @click="onAddCount">点我加1</button> -->
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    ...mapState("customAdd", ["count"]),
  },
  methods: {
    // onAddCount() {
    //   this.$store.dispatch("addCount", 1);
    // },
  },
};
</script>
  • mapMutations和mapState相似
<template>
  <div>
    <div>
      <span>{{ count }}</span>
    </div>
    <button @click="onAddCount(1)">点我加1</button>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    ...mapState("customAdd", ["count"]),
  },
  methods: {
    ...mapMutations("customAdd", { onAddCount: "ADD_COUNT" }),
  },
};
</script>
  • mapGetters、mapActions、mapMutations、mapState都相似写法
<template>
  <div>
    <div>
      <span>{{ count }}</span>
      <span>{{ expandCount }}</span>
    </div>
    <button @click="onAddCount(1)">点我加1</button>
  </div>
</template>

<script>
import { mapState, mapActions, mapGetters } from "vuex";
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    ...mapState("customAdd", ["count"]),
    ...mapGetters("customAdd", ["expandCount"]),
  },
  methods: {
    ...mapActions("customAdd", { onAddCount: "addCount" }),
  },
};
</script>
  • 不借助map,初始化写法如何写?整合展示,只有getters有些特别,展开getters,与state结构不同
    在这里插入图片描述
<template>
  <div>
    <div>
      <span>{{ count }}</span>
      <span>{{ expandCount }}</span>
    </div>
    <button @click="onAddCount">点我加1</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {};
  },
  computed: {
    count() {
      return this.$store.state.customAdd.count;
    },
    expandCount() {
      return this.$store.getters["customAdd/expandCount"];
    },
  },
  methods: {
    onAddCount() {
      // this.$store.commit("customAdd/ADD_COUNT", 1);
      this.$store.dispatch("customAdd/addCount", 1);
    },
  },
};
</script>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值