Vuex

0、vuex使用Module

// store
const moduleA = {
  namespaced: true, //使用命名空間
  state: { ... },
  getters: { ... },
  mutations: { ... },
  actions: { ... }
}
const moduleB = {
  state: { ... },
  getters: { ... }, //模塊內部的 getter,根節點狀態會做爲第三個參數暴露出來:(state, getters, rootState)
  mutations: { ... },
  actions: { ... } //actions內獲取根節點狀態:context.rootState
  //** 命名空間模塊能夠註冊全局action
    actions: {
        someAction: {
            root: true, //全局
            handler (namespacedContext, payload) { ... } // -> 'someAction'
        }
    }
}
const store = new Vuex.Store({
  modules: {
    moduleA,
    moduleB
  }
})
//命名空間
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('some/nested/module') // 建立基於某個命名空間輔助函數
複製代碼
  • 在 store 建立以後,你能夠使用 store.registerModule 方法註冊模塊。

一、state

// 獲取某個store
computed: {
    count () {
        return store.state.count
    }
}
// 獲取多個store
import { mapState } from 'vuex'
computed: mapState({
    count: state => state.count,
    countAlias: 'count',  // 傳字符串參數 'count' 等同於 `state => state.count`
    'count', // 映射 this.count 爲 store.state.count
    countPlusLocalState (state) {
      return state.count + this.localCount
    }
})
// store 與state computed 混合
computed: {
  localComputed(){},
  ...mapState({
    // ...
  })
}
複製代碼

二、getter (store 的計算屬性)

//定義
getters: {
    doneTodos: (state, getters) => {
        return state.todos.filter(todo => todo.done)
    },
    getTodoById: (state) => (id) => { //接收傳參
        return state.todos.find(todo => todo.id === id)
    }
}
// 獲取
store.getters.doneTodos //store內
computed: { 
  doneTodosCount () {
    return this.$store.getters.doneTodosCount //組件中
  }
}
//輔助函數
import { mapGetters } from 'vuex'
computed: {
    ...mapGetters([
       'doneTodosCount', //doneTodosCount: this.$store.getters.doneTodosCount
       'anotherGetter',
    ])
  }
複製代碼

三、Mutation (更改store狀態的惟一方法)

//註冊
mutations: {
  increment (state, payload) {
    state.count = payload.count
  }
}
//調用
store.commit('increment', {count: 10})
store.commit({ type: 'increment', amount: 10 })
//輔助函數
import { mapMutations } from 'vuex'
methods: {
    ...mapMutations([
      'increment', // increment(){ this.$store.commit('increment'); }
      'incrementBy' //傳參時incrementBy(amount){ this.$store.commit('incrementBy', amount) }
    ])
  }
複製代碼

四、 Action (提交mutation)

//註冊
actions: {
    increment (context) { //簡寫 {dispatch, commit,state, getters}
        const state = context.state; //獲取狀態
        const s = context.getters; //獲取getters
        context.commit('increment')
        return new Promise((resolve, reject)=>{
            //...
        });
    }
 }
 //調用
 store.dispatch('increment', {amount: 10})
 store.dispatch({ type: 'incrementAsync', amount: 10 })
 //輔助函數
 import { mapActions } from 'vuex'
 methods: {
    ...mapActions([
      'increment', // 將 `this.increment()` 映射爲 `this.$store.dispatch('increment')`
      'incrementBy' // 將 `this.incrementBy(amount)` 映射爲 `this.$store.dispatch('incrementBy', amount)`
    ])
  }
複製代碼

經常使用技巧

//雙向數據綁定
<input v-model="message">

computed: {
  message: {
    get () {
      return this.$store.state.obj.message
    },
    set (value) {
      this.$store.commit('updateMessage', value)
    }
  }
}
複製代碼
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息