vuex

什么是Vuex

Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理庫。

安裝和引入vuex

  • 安裝
yarn add vuex
  • 引入
    一般將項(xiàng)目應(yīng)用級(jí)的狀態(tài)都放到store中統(tǒng)一管理,項(xiàng)目新建一個(gè)store文件夾,這個(gè)里面存放所需的狀態(tài)
//store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
    state:{
        count:0
    },
    getters:{},
    mutations:{},
    actions:{}
})
export default store

定義好store后再main.js中引用

//main.js
import Vue from "vue";
import App from "./App.vue";
+ import store from "./store";
Vue.config.productionTip = false;
new Vue({
  render: (h) => h(App),
+  store,
}).$mount("#app");

核心概念

  • State

State中存儲(chǔ)公共數(shù)據(jù)。例如組件A和組件B共同用到的數(shù)據(jù)count,在組件A中修改count的值,組件B中count會(huì)隨之更改

  • 如何使用?兩種方式:
  1. 在模版中使用$store.state.count
//store中定義count
const store = new Vuex.Store({
  state: {
    count: 1,
  },
  getters: {
  },
  mutations: {},
  actions: {},
});
//模版中使用$store.state.count
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ $store.state.count }}</div>
  </div>
</template>
  1. 在計(jì)算屬性computed中使用
  • 在compute中定義
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ count }}</div>
  </div>
</template>
<script>
export default {
  name: "App",
  computed: {
    count(){
        return this.$store.state.count
    }
  },
};
</script>
  • 借助mapState輔助函數(shù)
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ count }}</div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "App",
  computed: {
    ...mapState(["count"]),
  },
};
</script>
  • Getters

存放從State中派生的一些狀態(tài)數(shù)據(jù),相當(dāng)于State的計(jì)算屬性,getter 的返回值會(huì)根據(jù)它的依賴被緩存起來,且只有當(dāng)它的依賴值發(fā)生了改變才會(huì)被重新計(jì)算。

  • 如何使用?兩種方式:
  1. 在模版中使用$store.getters.newCount
//store中定義
const store = new Vuex.Store({
  state: {
    count: 1,
  },
  getters: {
    newCount: (state) => {
      return state.count * 2;
    },
  },
  mutations: {},
  actions: {},
});
//模版中使用
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ $store.state.newCount}}</div>
    <div>{{ $store.getters.newCount}}</div>
  </div>
</template>
  1. 使用mapGetters輔助函數(shù)
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <!-- <div>{{ $store.state.count }}</div> -->
    <div>{{ count }}</div>
    <div>{{ newCount }}</div>
  </div>
</template>

<script>
import { mapState,mapGetters } from "vuex";
export default {
  name: "App",
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["newCount"]),
    
  },
};
</script>
  • Mutations

修改State的數(shù)據(jù)唯一方法就是commit一個(gè)mution中的方法,修改state時(shí)要調(diào)用store.commit(state,payload)提交一個(gè)payload來更改

  • 如何使用?兩種方式:
  1. 調(diào)用store.commit
//store中
const store = new Vuex.Store({
  state: {
    count: 1,
  },
  getters: {
    newCount: (state) => state.count * 2,
  },
  mutations: {
    addCount(state, payload) {
      setTimeout(() => {
        payload;
        state.count++;
      },1000);
    },
  },
  actions: {},
});
//模版中
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ count }}</div>
    <div>
      <button @click="handleClick">提交</button>
    </div>
    <div>{{ newCount }}</div>
  </div>
</template>

<script>
import { mapState, mapGetters,} from "vuex";
export default {
  name: "App",
  methods: {
    handleClick() {
      this.$store.commit({
        type: "addCount",
        num: parseInt(Math.random() * 10),
      });
    },
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["newCount"]),
  },
};
</script>
  1. 使用mapMutations輔助函數(shù)
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ count }}</div>
    <div>
      <button @click="addCount">提交</button>
    </div>
    <div>{{ newCount }}</div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations } from "vuex";
export default {
  name: "App",
  methods: {
    ...mapMutations(["addCount"]),
    handleClick() {
        this.addCount(Math.random() * 10)
    },
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["newCount"]),
  },
};
</script>

注意:mutation必須是同步函數(shù)

  • Actions

Actions和Mutations相似,不同的是:

  1. Actions提交的是一個(gè)mutation,而不是直接改變state
  2. Actions可以提交異步操作
  • 如何使用?兩種方式:
  1. 調(diào)用store.dispatch
//store中
const store = new Vuex.Store({
  state: {
    count: 1,
  },
  getters: {
    newCount: (state) => state.count * 2,
  },
  mutations: {
    addCount(state, payload) {
      state.count += payload.num;
    },
  },
  actions: {
    addCountAsync(context) {
      setTimeout(() => {
        const { commit } = context;
        commit("addCount", { num: 1 });
      });
    },
  },
});
//template中
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ count }}</div>
    <div>
      <button @click="action">action</button>
    </div>
    <div>{{ newCount }}</div>
  </div>
</template>
<script>
import { mapState, mapGetters, mapMutations } from "vuex";
export default {
  name: "App",
  methods: {
    action() {
      this.$store.dispatch("addCountAsync", { num: 1 });
    },
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["newCount"]),
  },
};
</script>
  1. 使用mapActions輔助函數(shù)
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <div>{{ count }}</div>
    <div>
      <button @click="action">action</button>
    </div>
    <div>{{ newCount }}</div>
  </div>
</template>

<script>
import { mapState, mapGetters, mapMutations, mapActions } from "vuex";
export default {
  name: "App",
  methods: {
    ...mapMutations(["addCount"]),
    ...mapActions(["addCountSync"]),
    handleClick() {
      // this.addCount({ num: parseInt(Math.random() * 10) });
      this.$store.commit({
        type: "addCount",
        num: 2,
      });
    },
    action() {
      // this.$store.dispatch("addCountAsync", { num: 1 });
      this.addCountSync({ num: 1 });
    },
  },
  computed: {
    ...mapState(["count"]),
    ...mapGetters(["newCount"]),
  },
};
</script>
  • Modules

當(dāng)所有數(shù)據(jù)都放入store中,當(dāng)應(yīng)用變得非常復(fù)雜時(shí),store 對象就有可能變得相當(dāng)臃腫。Modules的作用就是將store劃分為不同的模塊,每個(gè)Modules都包含state,getters,mutations,actions。這樣做方便代碼維護(hù),結(jié)構(gòu)更清晰

const moduleA = {
  state: () => ({ ... }),
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const moduleB = {
  state: () => ({ ... }),
  mutations: { ... },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a // -> moduleA 的狀態(tài)
store.state.b // -> moduleB 的狀態(tài)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Vuex五個(gè)概念的簡單理解。 1.State 保存共享狀態(tài)信息的地方。2.Getters 類似我們單個(gè)組件中的計(jì)算...
    似朝朝我心閱讀 981評論 0 6
  • Vue現(xiàn)代化使用方法(四)--Vuex 在組件內(nèi)可以通過data屬性共享數(shù)據(jù),父子組件也可以通過props進(jìn)行數(shù)據(jù)...
    去年的牛肉閱讀 227評論 0 2
  • 在組件內(nèi)可以通過data屬性共享數(shù)據(jù),父子組件也可以通過props進(jìn)行數(shù)據(jù)共享,但如果是兄弟跨組件之間的數(shù)據(jù)共享,...
    li4065閱讀 4,005評論 1 4
  • 1.什么是Vuex? 官方回答:Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式。它采用集中式存儲(chǔ)管...
    Angel_6c4e閱讀 434評論 0 1
  • https://www.cnblogs.com/samve/p/10726629.html vue:vuex中ma...
    丁先生_b64b閱讀 816評論 0 0

友情鏈接更多精彩內(nèi)容