Answer the question
In order to leave comments, you need to log in
Vue.js how to pass JSON to an application so that it is available in children?
I receive data from the server, the Vue application is embedded in the html page.
Depending on the data, I create the required number of vue instances.
JSON
{
"config": [{
"selector": ".first",
// .....................
},
{
"selector": ".second",
// .....................
}
]
}
let xhr = new XMLHttpRequest();
// ..........
xhr.onreadystatechange = function() {
if (xhr.status === 200) {
const dataConfig = JSON.parse(xhr.responseText);
// ...................
if (dataConfig.config.length > 1) {
for (let i = 0; i < dataConfig.config.length; i++) {
const object = dataConfig[i];
// .....................
new Vue({
el: object.selector,
data: {
appConfig: object
},
render: h => h(App)
})
}
}
}
}
Answer the question
In order to leave comments, you need to log in
If anyone needs it, I decided through vuex. On each cycle I create my own instance of the Vuex class and in the create of the Vue class I put the necessary information in the state Vuex
for (let i = 0; i < dataConfig.config.length; i++) {
const object = dataConfig[i];
let store = new Vuex.Store({
state: {},
mutations: {},
actions: {}
})
new Vue({
el: object.selector,
store,
created() {
this.$store.state.appConfig = object;
},
render: h => h(App)
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question