Vuex使用

model-manage.js

const state = {
    classificationObject: [],
    activeModel: {}
}

const getters = {
    models: state => {
        const models = []
        state.classificationObject.forEach(classification => {
            (classification['bk_objects'] || []).forEach(model => {
                models.push(model)
            })
        })
        return models
    },
    //返回一個函數(shù)
    getModelById: (state, getters) => id => {
        return getters.models.find(model => model['bk_obj_id'] === id)
    },
}

const mutations = {
    setActiveModel(state, model) {
        state.activeModel = model;
    },
    setClassificationObject(state, classificationObject) {
        state.classificationObject = classificationObject;
    }
}

const actions = {
   //返回promise
    findClassificationObject({ commit }) {
        return $api.bkApi.findClassificationObject().then(response => {
            commit('setClassificationObject', response);
            return response;
        });
    }

}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}

main.js:

import modelManage from './modules/views/model-manage.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
    modelManage
  }
})

使用state:

 var objectData = this.$store.state.modelManage.classificationObject;
 var activeModel = this.$store.state.modelManage.activeModel;
computed: {
    ...mapState({
      modelId: state => state.modelManage.activeModel.bk_obj_id,
      modelName: state => state.modelManage.activeModel.bk_obj_name
    })
  },
computed: mapState([
  // 映射 this.count 為 store.state.count
  'count'
])

使用mutations

 this.$store.commit("modelManage/setActiveModel", model);
 ...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')`
    })

使用getter

 ...mapGetters({
      getModelById: "modelManage/getModelById"
    }),
const model = this.getModelById()(this.$route.params.modelId);
const model = this.$store.getters["modelManage/getModelById"](
       this.$route.params.modelId );
computed: {
  // 使用對象展開運算符將 getter 混入 computed 對象中
    ...mapGetters([
      'doneTodosCount',
      'anotherGetter',
      // ...
    ])
  }

使用action

 ...mapActions({
      findClassificationObject: "modelManage/findClassificationObject"
    }),
await this.findClassificationObject();
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')`
    })
  }

組合 Action
(https://vuex.vuejs.org/zh/guide/state.html)

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

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

  • 安裝 npm npm install vuex --save 在一個模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,038評論 0 7
  • vuex中幾個核心概念: state, getters, mutations, actions, module g...
    IOneStar閱讀 14,107評論 3 13
  • Vuex是什么? Vuex 是一個專為 Vue.js 應(yīng)用程序開發(fā)的狀態(tài)管理模式,方便數(shù)據(jù)管理,避免數(shù)據(jù)重復(fù)加載,...
    席坤閱讀 548評論 0 0
  • vuex官網(wǎng) 概念,優(yōu)缺點,就不多說了。vuex直白點說就是封裝的一個數(shù)據(jù)共享的對象注入到vue根部,然后多界面共...
    偏愛墨綠色閱讀 641評論 0 1
  • vuex是一個專門為vue構(gòu)建的狀態(tài)管理工具,主要是解決多組建狀態(tài)共享的問題。強(qiáng)調(diào)的是集中式管理(組件與組件之間的...
    離陌Study閱讀 8,537評論 0 4

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