假如用王者荣耀的方式学习Vuex

维佑 · 爱克斯
姓名:维佑·爱克斯(vuex)
热度排名:T0
胜率:95%
登场率:90%(中大型项目100%)
Ban率:0%
01 Status(单一状态树)

02 Getter(计算属性)

03 Mutation(出尔反尔)

04 Action(异步操作)

05 Module(多重影分身)

01 mapState

箭头函数返回
count: state => state.countcountAlias 传递字符串参数
countAlias: 'count'传入数组
computed: mapState([// 映射 this.count 为 store.state.count 'count' ])02 mapMutation

methods:{...mapMutations(['increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`]),}
03 mapActions

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

store.dispatch('increment')05 commit

store.commit('increment')
爱克斯的个人原则
应用层级的状态应该集中到单个 store 对象中。 提交 mutation 是更改状态的唯一方法,并且这个过程是同步的。 异步逻辑都应该封装到 action 里面。
爱克斯如何处理表单
:value="message" @input="updateMessage">methods: {updateMessage (e) {this.$store.commit('updateMessage', e.target.value)}}2. 使用 computed 计算属性的 get 和 set 方法做对应处理、
<input v-model="message">
computed: {message: {get () {return this.$store.state.obj.message},set (value) {this.$store.commit('updateMessage', value)}}}
评论
