Vuex

state

this.$store.state.   //獲取

放在computed里

// 在單獨(dú)構(gòu)建的版本中輔助函數(shù)為 Vuex.mapState
import { mapState } from 'vuex'

export default {
  // ...
  computed: mapState({
    // 箭頭函數(shù)可使代碼更簡練
    count: state => state.count,

    // 傳字符串參數(shù) 'count' 等同于 `state => state.count`
    countAlias: 'count',

    // 為了能夠使用 `this` 獲取局部狀態(tài),必須使用常規(guī)函數(shù)
    countPlusLocalState (state) {
      return state.count + this.localCount
    }
  })
}
computed: mapState([
  // 映射 this.count 為 store.state.count
  'count'
])

對象展開運(yùn)算符

computed: {
  localComputed () { /* ... */ },
  // 使用對象展開運(yùn)算符將此對象混入到外部對象中
  ...mapState({
    // ...
  })
}

Getter

對于state的計(jì)算屬性

//normal
computed: {
  doneTodosCount () {
    return this.$store.state.todos.filter(todo => todo.done).length
  }
}

//getter
const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    }
  }
})
this.$store.doneTodos

getter嵌套getter

getters: {
  // ...
  doneTodosCount: (state, getters) => {
    return getters.doneTodos.length
  }
}
this.$store.getters.doneTodosCount

mapGetters

import { mapGetters } from 'vuex'

export default {
  // ...
  computed: {
  // 使用對象展開運(yùn)算符將 getter 混入 computed 對象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }
}
...mapGetters({
  // 把 `this.doneCount` 映射為 `this.$store.getters.doneTodosCount`
  doneCount: 'doneTodosCount'
})

Mutation

const store = new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state) {
      // 變更狀態(tài)
      state.count++
    }
  }
})
store.commit('increment')

傳參

mutations: {
  increment (state, n) {
    state.count += n
  }
}
store.commit('increment', 10)

payload

// ...
mutations: {
  increment (state, payload) {
    state.count += payload.amount
  }
}
store.commit('increment', {
  amount: 10
})
state.obj = { ...state.obj, newProp: 123 }

組件中提交Mutation

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')`
    })
  }
}

Action

Modules

const moduleA = {
    namesapced=true,
    // ...
    actions: {
        incrementIfOddOnRootSum({ state, commit, rootState }) {
            if ((state.count + rootState.count) % 2 === 1) {
                commit('increment')
            }
        },
        someAction({ dispatch, commit, getters, rootGetters }) {
            getters.someGetter // -> 'foo/someGetter'
            rootGetters.someGetter // -> 'someGetter'

            dispatch('someOtherAction') // -> 'foo/someOtherAction'
            dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'

            commit('someMutation') // -> 'foo/someMutation'
            commit('someMutation', null, { root: true }) // -> 'someMutation'
        },
        // 注冊全局的,不推薦使用
        wholeAction: {
            root: true,
            handler() { }
        }
    }
}

使用時(shí)

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

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

  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 789評論 0 3
  • 習(xí)慣養(yǎng)成很容易,戒掉卻很難?。。?什么是Vuex? Vuex 是一個(gè)專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式...
    前端又又閱讀 2,859評論 0 1
  • 學(xué)前準(zhǔn)備 本文主要內(nèi)容來源于官網(wǎng),為Vuex的基礎(chǔ)部分總結(jié),部分案例也參考了互聯(lián)網(wǎng)上其他分享知識的大佬。本手記結(jié)合...
    橙色流年閱讀 407評論 0 1
  • 安裝 npm npm install vuex --save 在一個(gè)模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,049評論 0 7
  • 使用說明-Vuex 安裝 直接下載 / CDN 引用 Unpkg.com 提供了基于 NPM 的 CDN 鏈接。以...
    滿是裂縫的花卷閱讀 1,081評論 0 8

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