Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
callApi('towns', queryParams, '').then((res) => { this.cities = res.data.data; })
Vue.set(self, 'cities', res.data.data)
callApi('towns', queryParams, '').then((res) => { Vue.set(self, 'cities', res.data.data); })
actionName(context, data) { //... }
//.. this.$store.dispatch('actionName', res.data.data);
export const actions = { fetchSities (state, {queryParams}) { callApi ('towns', queryParams,'').then((res) => { state.commit('setSities', Vue.set(self, 'cities', res.data.data)) }) }, }
//state cities: [] //mutations set_sities(state, payload) { state.cities = payload; } //actions... fetchSities(context, params) { return new Promise((resolve, reject) => { callApi('towns', params).then(res => { context.commit('set_sities', res.data.data); resolve(res); //! }).catch(error => reject); }); }
//где нибудь var params = { ... }; this.$store.dispatch('fetchSities', params).then(res => { console.log(res); //ответ от api console.log(this.$store.state.cities); //массив с данными });
Uncaught TypeError: Cannot read property 'dispatch' of undefined
//store/sities import Axios from 'axios' import callApi from '~/libraries/api.js' export const state = () => ({ cities: [], }) export const actions = { fetchSities (state, {queryParams}) { return new Promise((resolve, reject) => { callApi('towns', params).then(res => { context.commit('setSities', res.data.data); resolve(res); //! }).catch(error => reject); }) }, } export const mutations = { setSities: (state, data) => { state.cities = Object.assign({}, data) }, }
//внутри компонента computed: { cities () { return this.$store.state.sities.cities }, }, this.$store.dispatch('sities/fetchSities', {queryParams}).then(res => { Vue.set(self, 'cities', this.$store.state.sities.cities); });
//хук mounted mounted() { this.$store.dispatch('sities/fetchSities', {queryParams}).then(res => { Vue.set(self, 'cities', this.$store.state.sities.cities); }); }
this.$store.dispatch('sities/fetchSities', {queryParams}).then(res => { Vue.set(self, 'cities', this.$store.state.sities.cities); });