Простейшим способом было бы заставить ваши компоненты (явно) не заботиться о том, были ли выбраны календари.
Просто скажите магазину, чтобы он покупал, но действие решает, действительно ли это необходимо.
Предполагается, что Vuex является односторонним потоком данных, поэтому информация о состоянии не возвращается от действия к компоненту, вместо этого компонент всегда просто ожидает поступления данных.
Чтобы сделать вещи реактивными, используйте комбинацию computed
и getter
.
1012 * компонент *
created() {
/* tell the store 'this is what I need' */
store.dispatch('calendar/getCalendars');
},
...
computed: {
calendar() {
/* reactive - initially null, then gets a value when the store is updated */
return this.$store.getters['calendar/calendarById'](this.$route.params.id)
},
},
магазин
getters: {
calendarById: (state) => {
/* return a function so that param can be passed from component */
return (id) => state.calendars ? state.calendars.find(c => c.id === id) : null;
},
}
actions: {
getCalendars (store) {
/* only perform the fetch if the store does not already contain the data */
if (!store.state.calendars) {
fetch(calendarsUrl).then(res => store.commit('setCalendars', res); // e.g
}
},
}