如何使Vuex吸气剂具有反应性

时间:2018-06-24 13:56:20

标签: javascript vue.js vuex

我正在与VUE一起制定时间表,我对VUE和JS真的很陌生。我创建了VUE组件,该组件将一些新属性添加到已经处于状态的对象中,然后使用吸气剂将该对象带入另一个VUE组件中进行渲染,但是不幸的是,新属性仅在重新加载后才呈现。 这是我在其中添加新属性的组件的片段

    methods: {
                ...mapActions({addDateToState: 'addDate'}),
                addDate () {
                    this.date = this.startTime.time; 
                    //date from datepicker

                    this.addDateToState({date:this.date, year:this.year, time:this.event.time, name:this.event});
                    // object need to add
                },

            }

这是国家的摘录

const state = {
    schedule: {}
}

这是动作

addDate ({commit}, date) {
        commit('ADD_DATE', date)
    }

这是完成所有工作的变异

    ADD_DATE (state, date) {
            if (typeof state.schedule[date.year] === 'undefined') {
                state.schedule[date.year] = {};
            }

            if (typeof state.schedule[date.year][date.date] === 'undefined') {
                state.schedule[date.year][date.date] = {};
            }
            if (typeof state.schedule[date.year][date.date][date.time] === 'undefined') {
                state.schedule[date.year][date.date][date.time] = [];
            }
            state.schedule[date.year][date.date][date.time].push(date.name)
            //interesting that this properties are reactive, so I see chenges when pushh new element to the array, but don't see if it is new property on the object

            console.log(state.schedule)
            //here I see the schedule which already has new properties so mutation works

        }

信件

const getters = {
schedule() {
        return state.schedule;
    }
}

这是计算属性,可从getter获取时间表

computed: {
    ...mapGetters([
        'schedule'
    ])
}

所以我无法使此吸气剂具有反应性,但我看到状态已更改。有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

如果要添加新属性,则需要使用Vue.set / this。$ set。参见vue docs

答案 1 :(得分:-1)

我,当我需要使用反应性吸气剂时,可以这样做:

computed: {
    getSchedule: function () { return this.schedule; }
}