Vuex(五) 传递参数

1. 事例代码

(1)src/vuex/store.js中

    // actions中传递参数
    const mutations = {
        ADD (state, n) {
            state.count += n;
        }
    }
    // actions中传递参数
    const actions ={
        // 触发mutations中相应的方法
        add ({commit}, n) {
            // 增加异步
            setTimeout(()=>{
                commit('ADD', n);
            },3000);
            console.log('我比reduce提前执行');
        }
    }

(2)页面组件常规调用传递

    // template
    <button @click="add">+</button>
    // script
    methods: {
        add() {
            // 分发action
            this.$store.dispatch('add', 99);
        }
    }

(3)页面组件使用mapActions调用传递

    // template
    <button @click="add(99)">+</button>
    // script
    methods: {
        ...mapActions(['add'])
    }

 上一篇
Vuex(六) module-模块组 Vuex(六) module-模块组
当应用非常复杂,状态非常多的时候,需要将store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块,从上至下进行同样方式的分割。
2019-08-08
下一篇 
Vuex(四) mutations-getters-actions异步 Vuex(四) mutations-getters-actions异步
更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。Vuex 中的 mutation 非常类似于事件:每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)
2019-08-03
  目录