V
V
Vanya Zyuzgin2019-06-21 16:55:32
Vue.js
Vanya Zyuzgin, 2019-06-21 16:55:32

How to download the initial values ​​of variables from the server using Vuex?

There is a store.js file for connecting the storage, in which you need to load data from a third-party server into the company variable via Axios. You need to do this at the start of the application.

export default new Vuex.Store({
  state: {
    company: [],
  }
})

It only comes to mind to put this loading into a separate component and do it in mounted . But, it seems to me that this is wrong. How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor L, 2019-06-21
@site2life

as an option in main.js in the beforeCreate hook, perform the initialization of filling the store

new Vue({
  el: "#app",
  router,
  store,
  beforeCreate() {
    this.$http
      .jsonp(getURL(method, { offset: 1, album_id: 0, extended: 1 }))
      .then(response => {
        return response.json();
      })
      .then(data => {
        return data.response.items.map(items => {
        ///
          return items;
        });
      })
      .then(data => {
        this.$store.dispatch("getData", data);
      });
  {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question