使用vuex实现组件间传值

1.state中的数据获取时使用 this.$store.state.xxx;
2.修改state中的数据必须通过 mutations 中提供的方法,调用方法使用 this.$store.commit('方法名称', 唯一参数),要传递多个参数可使用对象;
3.如果对外提供 state 中的数据时需要对数据进行包装,可使用 getters,方法为: this.$store.getters.xxx
let store = new Vuex.Store({
    state: {
        theUrl: {},
        count: 0,
    },
    mutations: {
        increment(state, obj) {
        state.count += (obj.a + obj.d)
         }   
     },
    getters: {
        optCount: function (state) {
        return '当前最新的count值是:' + state.count
        }
  },
}

猜你喜欢

转载自www.cnblogs.com/Ryan368/p/11332073.html
今日推荐