vuex的两种使用方法

本文通过模块化使用方法来介绍

在srore文件创建vuexuse文件夹,并在该文件夹下创建index.js文件
在这里插入图片描述

在index文件写需要的数据

export default {
    namespaced: true,
    state: {
        num: 100,
        numtwo:200
    },
    getters: {
        doubleCount(state) {
            return state.num * 2;
        },
        doubleCounttwo(state) {
            return state.numtwo * 3;
        }
    },
    mutations: {
        getmmutsttos(state, apply) {
            state.num += apply
        },
        getmmutsttostwo(state, apply) {
            state.numtwo += apply
        }
    },
    actions: {
        setactionsend(context, apply) {
            context.commit('getmmutsttos', apply)
        },
        setactionsendtwo(context, apply) {
            context.commit('getmmutsttostwo', apply)
        }
    }
}

在store文件夹下的index.js注册vuexuse模块

import Vue from 'vue'
import Vuex from 'vuex'
import vuexuse from './vuexuse'
Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    vuexuse
  }
})

然后按照下面方式使用即可

<template>
  <div class="container">
    <div class="one">
      <h3>第一种原始的使用方法</h3>
      <div>state的值:{{ $store.state.vuexuse.num }}</div>
      <div>getter的double值:{{ thisDoubleCount }}</div>
      <div><button @click="update">mutations修改state的值+10</button></div>
      <div><button @click="ybupdate">异步actions的值+100</button></div>
    </div>
    <div class="one two">
      <h3>第二种map方式的使用方法</h3>
      <div>state的值:{{ numtwo }}</div>
      <div>getter的3倍值:{{ doubleCounttwo }}</div>
      <div><button @click="updatetwo">mutations修改state的值+20</button></div>
      <div><button @click="ybupdatetwo">异步actions的值+200</button></div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
export default {
  data() {
    return {};
  },
  created() {
    document.title = "vuex的两种使用方法使用";
  },
  methods: {
    ...mapMutations("vuexuse", ["getmmutsttostwo"]),
    ...mapActions("vuexuse", ["setactionsendtwo"]),
    update() {
      this.$store.commit("vuexuse/getmmutsttos", 10);
    },
    ybupdate() {
      this.$store.dispatch("vuexuse/setactionsend", 100);
    },
    updatetwo() {
      this.getmmutsttostwo(20);
    },
    ybupdatetwo() {
      this.setactionsendtwo(200);
    },
  },
  computed: {
    ...mapState("vuexuse", ["numtwo"]),
    ...mapGetters("vuexuse", ["doubleCounttwo"]),
    thisDoubleCount() {
      return this.$store.getters["vuexuse/doubleCount"];
    },
  },
  components: {},
};
</script>

<style scoped lang='less'>
.container {
  .one {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 10px;
  }
  .two {
    margin-top: 20px;
  }
  button {
    margin: 10px;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值