0๐
-
Create a copy of routes of charts in
store
const state = { chartsRoutes: [] }
-
Create a
computed
property in componentcomputed: { chartsRoutes () { return this.$store.state.chartsRoutes } }
-
Use
v-for
to renderchartsRoutes
intorouter-link
s in component -
Create a mutation to modify store and router
// import router const mutations = { 'update-charts-routes': function (state, payload) { const { chartsRoutes } = payload state.chartsRoutes = chartsRoutes.map(r => { return { path: '/your/custom/path/according/to/response' // other params } }) router.addRoutes(state.chartsRoutes) } }
-
Create an action
const actions = { 'reload-charts': function ({commit, dispatch}, data) { return new Promise((resolve, reject) => { const r = { method: 'get', url: data.url, // add more options, e.g. header or auth } axios.request(r) .then(resp => { commit('update-charts-routes', { chartsRoutes: resp.data }) resolve() }) .catch(err => { // handle error reject(err) }) } } } }
-
Dispatch action
this.$store.dispatch('reload-charts', { url: 'http://some.host/path/to/url' }) .then(() => { // other stuff })
Source:stackexchange.com