vuex一个demo轻松理解

 1.安装vuex

$ npm install vuex --save

2.在src目录下新建store文件和store.js

3.store.js

// store.js
import Vue from 'vue'
import VueX from 'Vuex'

Vue.use(VueX);

const store = new VueX.Store({
  //state属性用来存储状态值
  state: {
    count: 0,
    name: ''
  },
  //getters属性用来获取状态值,相当于SPA页面中的computed属性
  getters: {
    count: function (state) {
      return state.count;
    },
    name: function(state) {
        if(state.name == 'doinb'){
            state.name= 'doinb6'
            return state.name
        }
    }
  },
  //mutations用来提供改变状态的方法,只能执行同步方式
  mutations: {
    increment: function (state) {
      state.count ++;
    },

    decrement: function (state) {
      if (state.count == 0) {
        state.count = 0;
      } else {
        state.count --;
      }
    },
    changeName1(state, name) {
      state.name = name
    },
    changeName2(state, name) {
      state.name = name
    }
  },

  //actions接收对应的指令,去mutations中执行,能够执行异步方式

  actions: {
    changeName2(context, name) {
      setTimeout(() => {
        context.commit("changeName2", name);
      }, 3000)
    },
  }


})
export default store;

4.在项目入口文件引入store.js,记住别忘记在vue实例下注册store

5.demo.vue

<!-- vuex -->
<template>
    <div>
        <h1>{{name}}</h1>
        <button @click="decrement"> - </button>
        <input type="text" v-model="count">
        <button @click="increment"> + </button>
    </div>
</template>

<script>
export default {
  data() {
    return {};
  },

  computed: {
    name() {
    //   return this.$store.getters.name;  // getters方式获取值
     return this.$store.state.name;
    },
    count() {
      return this.$store.state.count;
    }
  },
  components: {},

  mounted() {},

  methods: {
    increment() {
      this.$store.commit("increment");
      this.changeName1();
    },
    decrement() {
        this.$store.commit("decrement");
        this.changeName2();
    },
    changeName1() {
        this.$store.commit("changeName1", '叫我官人');  //mutations 方式
    },
    changeName2() {
        this.$store.dispatch("changeName2", 'doinb');  // actions 方式
    }
  }
};
</script>
<style scoped>
</style>

 

好的,下面是一个简单的在Vue2中使用Vuexdemo: 1. 首先,需要安装vuex: ``` npm install vuex --save ``` 2. 创建一个store.js文件,用于定义Vuex的state、mutations、actions和getters: ```javascript import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const state = { count: 0 }; const mutations = { INCREMENT(state) { state.count++; }, DECREMENT(state) { state.count--; } }; const actions = { increment({ commit }) { commit('INCREMENT'); }, decrement({ commit }) { commit('DECREMENT'); } }; const getters = { doubleCount: state => state.count * 2 }; export default new Vuex.Store({ state, mutations, actions, getters }); ``` 3. 在main.js中引入store.js,并将其挂载到Vue实例上: ```javascript import Vue from 'vue'; import App from './App.vue'; import store from './store'; new Vue({ el: '#app', store, render: h => h(App) }); ``` 4. 在组件中使用Vuex的state、mutations、actions和getters: ```javascript <template> <div> <p>Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click="increment">Increment</button> <button @click="decrement">Decrement</button> </div> </template> <script> import { mapState, mapActions, mapGetters } from 'vuex'; export default { computed: { ...mapState(['count']), ...mapGetters(['doubleCount']) }, methods: { ...mapActions(['increment', 'decrement']) } }; </script> ``` 以上就是一个简单的在Vue2中使用Vuexdemo,通过这个demo,我们可以学习到如何在Vue2中使用Vuex来管理组件的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值