维佑·爱克斯是鲁班大师创造出来的三代机器人,目前负责稷下学院负责学院物资分配工做,他采用集中式存储管理着学院的全部的物资,并以相应的规则保证物资以一种可预测的方式发生变化。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)
}
}
}
复制代码
本期英雄介绍完毕,祝你们早日国服王者,咱们下期见。