programing

vuex getter를 반응시키는 방법

coolbiz 2022. 8. 17. 22:25
반응형

vuex getter를 반응시키는 방법

VUE랑 스케줄 짜고 있는데 VUE랑 JS랑 정말 처음이에요.이미 상태가 된 개체에 몇 가지 새 속성을 추가하는 VUE 구성 요소를 만든 다음 이 개체를 getter로 가져와서 다른 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;
    }
}

그리고 여기 getters에서 스케줄을 가져오는 계산된 속성이 있습니다.

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

문제는 이 게터를 반응시킬 수는 없지만 상태가 바뀌었다는 것입니다.누가 나 좀 도와줄래?

Vue.set/this를 사용해야 합니다.$set (새로운 속성을 추가할 경우)"vue 문서"

vue에서 반응성 getter가 필요할 때 다음 작업을 수행합니다.

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

언급URL : https://stackoverflow.com/questions/51010591/how-to-make-vuex-getters-reactive

반응형