19-Vuex
1 | 传参的方法对于多层嵌套的组件将会非常繁琐,并且对于兄弟组件间的状态传递无能为力 |
1 | 采用父子组件直接引用或者通过事件来变更和同步状态的多份拷贝,通常会导致无法维护的代码 |
1 | // 创建一个 Counter 组件 |
1 | const app = new Vue({ |
1 | const Counter = { |
1 | // 在单独构建的版本中辅助函数为 Vuex.mapState |
1 | computed: mapState([ |
1 | computed: { |
1 | const store = new Vuex.Store({ |
1 | store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }] |
1 | getters: { |
1 | computed: { |
1 | getters: { |
1 | import { mapGetters } from 'vuex' |
1 | mapGetters({ |
1 | const store = new Vuex.Store({ |
1 | // ... |
1 | store.commit('increment', 10) |
1 | ... |
1 | // 以载荷形式分发 |
1 | // 以对象形式分发 |
1 | mutations: { |
1 | // mutation-types.js |
1 | export default { |
1 | // store.js |
1 | ... |
1 | import MutationType from './mutation-type' |
1 | mutations: { |
1 | this.$store.commit('increment','参数') |
1 | import { mapMutations } from 'vuex' |
1 | const store = new Vuex.Store({ |
1 | actions: { |
1 | store.dispatch('increment') |
1 | actions: { |
1 | // 以载荷形式分发 |
1 | actions: { |
1 | import { mapActions } from 'vuex' |
1 | actions: { |
1 | store.dispatch('actionA').then(() => { |
1 | actions: { |
1 | // 假设 getData() 和 getOtherData() 返回的是 Promise |
1 | const moduleA = { |
1 | const moduleA = { |
1 | const moduleA = { |
1 | const moduleA = { |
1 | const store = new Vuex.Store({ |
1 | /** |
1 | /** |
1 | /** |
1 | import * as types from "./mutations-types" |
1 | import Vue from 'vue' |
1 | import Vue from 'vue' |
1 | <template> |
1 | <div id="example"> |
1 | var vm = new Vue({ |
1 | // src/core/instance/state.js |
1 | // src/core/instance/state.js |
1 | // src/core/observer/index.js |
1 | // src/core/observer/index.js |
1 | // src/core/observer/dep.js |
1 | // src/core/instance/state.js |
1 | // src/core/observer/watcher.js |
1 | this.dirty = this.lazy this.getter = expOrFn |
1 | // src/core/instance/state.js |
1 | // src/core/instance/state.js |
1 | get: function reactiveGetter () { |
1 | var vm = new Vue({ |
1 | // src/store.js |
1 | // src/mixins.js |
1 | const store = new Vuex.Store({ |
1 | // src/store.js |
1 | // src/store.js |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Estom的博客!








