1
votes

I working on a schedule with VUE, I'm really new with VUE and with JS. I created VUE component that adds some new properties into the object which already in state and then I take this object with getters and render inside my another VUE component, but unfortunately new properties render only after reloading. Here is snippet from my component where I add new property

    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
                },

            }

Here is a snippet from state

const state = {
    schedule: {}
}

Here is actions

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

And here is the mutation which does all the work

    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

        }

Getters

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

And here is computed property which get the schedule from getters

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

So the problem that I can't make this getter reactive, but I see that state was changed. Can someone help me?

2

2 Answers

3
votes

You need to use Vue.set/this.$set if you are adding new property. See vue docs

-1
votes

me, when i need a reactive getter in vue a do that:

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