B
B
be_a_man2019-01-20 09:44:31
Vue.js
be_a_man, 2019-01-20 09:44:31

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",
            // .....................
        }
    ]
}

JS something like this + different checks
How to use appConfig in child components?
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

1 answer(s)
B
be_a_man, 2019-01-20
@be_a_man

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 question

Ask a Question

731 491 924 answers to any question