維佑·愛克斯是魯班大師創造出來的三代機器人,目前負責稷下學院負責學院物資分配工做,他採用集中式存儲管理着學院的全部的物資,並以相應的規則保證物資以一種可預測的方式發生變化。vue
姓名:維佑·愛克斯(vuex)vuex
熱度排名:T0數組
勝率:95%緩存
登場率:90%(中大型項目100%)bash
Ban率:0%異步
愛克斯經過modules,能夠將store分割成模塊。每一個模塊擁有本身的state等屬性;函數
count: state => state.count
* countAlias傳遞字符串參數
countAlias: 'count'
* 傳入數組
computed: mapState([ // 映射 this.count 爲 store.state.count 'count' ])
methods:{
...mapMutations([
'increment', // 將 `this.increment()` 映射爲 `this.$store.commit('increment')`
]),
}
複製代碼
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')`
})
}
複製代碼
store.dispatch('increment')
複製代碼
store.commit('increment')
複製代碼
應用層級的狀態應該集中到單個 store 對象中。ui
提交 mutation 是更改狀態的惟一方法,而且這個過程是同步的。this
異步邏輯都應該封裝到 action 裏面。spa
<!--view-->
<input :value="message" @input="updateMessage">
<!--methods-->
methods: {
updateMessage (e) {
this.$store.commit('updateMessage', e.target.value)
}
}
複製代碼
<input v-model="message">
複製代碼
computed: {
message: {
get () {
return this.$store.state.obj.message
},
set (value) {
this.$store.commit('updateMessage', value)
}
}
}
複製代碼
本期英雄介紹完畢,祝你們早日國服王者,咱們下期見。