摘要:管理統(tǒng)一組件狀態(tài)。映射到組件將映射為也支持載荷將映射為將映射為概念允許我們將分割成模塊。每個(gè)模塊擁有自己的類似里面的針對(duì)每個(gè)組件對(duì)應(yīng)的狀態(tài)做處理的狀態(tài)的狀態(tài)對(duì)于模塊內(nèi)部的,局部狀態(tài)通過暴露出來,根節(jié)點(diǎn)狀態(tài)則為。
1.首先明白vuex是做什么用的。
管理統(tǒng)一組件狀態(tài)state。每個(gè)應(yīng)用將僅僅包含一個(gè) store 實(shí)例。單一狀態(tài)樹讓我們能夠直接地定位任一特定的狀態(tài)片段,在調(diào)試的過程中也能輕易地取得整個(gè)當(dāng)前應(yīng)用狀態(tài)的快照。2.如何實(shí)現(xiàn) vuex有幾個(gè)對(duì)象
state =>mapState
getters =>mapGetters
actions =>mapActions
mutations => mapMutations
moudle
3.state (注入store)Vuex 通過 store 選項(xiàng),提供了一種機(jī)制將狀態(tài)從根組件“注入”到每一個(gè)子組件中(需調(diào)用 Vue.use(Vuex)):
const app = new Vue({ el: "#app", // 把 store 對(duì)象提供給 “store” 選項(xiàng),這可以把 store 的實(shí)例注入所有的子組件 store, components: { Counter }, template: `` })
通過在根實(shí)例中注冊(cè) store 選項(xiàng),該 store 實(shí)例會(huì)注入到根組件下的所有子組件中,且子組件能通過 this.$store 訪問到。
當(dāng)我們需要時(shí)刻跟蹤狀態(tài)值的變化時(shí),可以通過組件的計(jì)算機(jī)屬性來確定,然后使用mapState.方法來映射。
computed: { localComputed () { /* ... */ }, // 使用對(duì)象展開運(yùn)算符將此對(duì)象混入到外部對(duì)象中 ...mapState({ // ... }) }4.getters (都是方法)
Vuex 允許我們?cè)?store 中定義“getter”(可以認(rèn)為是 store 的計(jì)算屬性)。就像計(jì)算屬性一樣,getter 的返回值會(huì)根據(jù)它的依賴被緩存起來,且只有當(dāng)它的依賴值發(fā)生了改變才會(huì)被重新計(jì)算。
Getter 接受 state 作為第一個(gè)參數(shù)
mapGetters 輔助函數(shù)僅僅是將 store 中的 getter
import { mapGetters } from "vuex" export default { computed: { // 使用對(duì)象展開運(yùn)算符將 getter 混入 computed 對(duì)象中 ...mapGetters([ "doneTodosCount", "anotherGetter", // ... ]) } }5. mutation
更改 Vuex 的 store 中的狀態(tài)的唯一方法是提交 mutation,調(diào)用方法store.commit("increment")
注意點(diǎn)
使用常量替代 Mutation 事件類型
export const SOME_MUTATION = "SOME_MUTATION" // store.js import Vuex from "vuex" import { SOME_MUTATION } from "./mutation-types" const store = new Vuex.Store({ state: { ... }, mutations: { // 我們可以使用 ES2015 風(fēng)格的計(jì)算屬性命名功能來使用一個(gè)常量作為函數(shù)名 [SOME_MUTATION] (state) { // mutate state } } })
Mutation 必須是同步函數(shù)
mapMutations
import { mapMutations } from "vuex" export default { // ... methods: { ...mapMutations([ "increment", // 將 `this.increment()` 映射為 `this.$store.commit("increment")` // `mapMutations` 也支持載荷: "incrementBy" // 將 `this.incrementBy(amount)` 映射為 `this.$store.commit("incrementBy", amount)` ]), ...mapMutations({ add: "increment" // 將 `this.add()` 映射為 `this.$store.commit("increment")` }) } }6.Action
Action 提交的是 mutation,而不是直接變更狀態(tài)。
Action 可以包含任意異步操作。
context 不是 store 實(shí)例本身
Action 函數(shù)接受一個(gè)與 store 實(shí)例具有相同方法和屬性的 context 對(duì)象,因此你可以調(diào)用 context.commit 提交一個(gè) mutation,或者通過 context.state 和 context.getters 來獲取 state 和 getters。
actions: { increment ({ commit }) { commit("increment") } }
mapActions 映射到組件
import { mapActions } from "vuex" export default { // ... methods: { ...mapActions([ "increment", // 將 `this.increment()` 映射為 `this.$store.dispatch("increment")` // `mapActions` 也支持載荷: "incrementBy" // 將 `this.incrementBy(amount)` 映射為 `this.$store.dispatch("incrementBy", amount)` ]), ...mapActions({ add: "increment" // 將 `this.add()` 映射為 `this.$store.dispatch("increment")` }) } }7. Module
概念
Vuex 允許我們將 store 分割成模塊(module)。每個(gè)模塊擁有自己的 state、mutation、action、getter.類似redux里面的reducer 針對(duì)每個(gè)組件對(duì)應(yīng)的state狀態(tài)做處理
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)
rootState
對(duì)于模塊內(nèi)部的 action,局部狀態(tài)通過 context.state 暴露出來,根節(jié)點(diǎn)狀態(tài)則為 context.rootState。
rootState 可以獲取到所有mudule里面的state值
const moduleA = { // ... actions: { incrementIfOddOnRootSum ({ state, commit, rootState }) { if ((state.count + rootState.count) % 2 === 1) { commit("increment") } } } }
這個(gè)還是要多看官方的demo
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/93929.html
摘要:配置配置使用概率抽樣。采樣率定義了對(duì)跟蹤跨度進(jìn)行采樣的概率,其值可以介于和含之間。例如,以下配置對(duì)象將采樣率更改為即每個(gè)跨度都被采樣,并使用協(xié)議將跟蹤發(fā)送到位于的服務(wù)器文件路徑注將采樣率更改為會(huì)完全禁用跟蹤。目錄手把手教你學(xué)Dapr - 1. .Net開發(fā)者的大時(shí)代手把手教你學(xué)Dapr - 2. 必須知道的概念手把手教你學(xué)Dapr - 3. 使用Dapr運(yùn)行第一個(gè).Net程序手把手教你學(xué)Da...
摘要:組件聲明組件分為全局的和局部的。父組件通過給子組件下發(fā)數(shù)據(jù),子組件通過事件給父組件發(fā)送消息。以下實(shí)例中子組件已經(jīng)和它外部完全解耦了。 1.vue 組件-聲明 組件分為全局的和局部的。 全局聲明 Vue.component(tagName, options) ** 引用組件 // 注冊(cè) Vue.comp...
摘要:組件聲明組件分為全局的和局部的。父組件通過給子組件下發(fā)數(shù)據(jù),子組件通過事件給父組件發(fā)送消息。以下實(shí)例中子組件已經(jīng)和它外部完全解耦了。 1.vue 組件-聲明 組件分為全局的和局部的。 全局聲明 Vue.component(tagName, options) ** 引用組件 // 注冊(cè) Vue.comp...
閱讀 3343·2023-04-25 19:42
閱讀 1383·2021-11-23 10:11
閱讀 2338·2021-11-16 11:51
閱讀 1631·2019-08-30 15:54
閱讀 2080·2019-08-29 18:44
閱讀 1655·2019-08-23 18:24
閱讀 521·2019-08-23 17:52
閱讀 1804·2019-08-23 15:33