A
A
Antonio Solo2019-01-22 09:57:40
Vue.js
Antonio Solo, 2019-01-22 09:57:40

How to initialize a VUE component?

I want to use VUE on the pages of the site, the component requires a lot of data from the database, and each page has its own data.
How to correctly build the initialization of the initial state of the component, without using ajax requests?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Drozdov, 2019-01-22
@solotony

Well, in fact, the approach with window.__INITIAL_STATE__ is described here, you put json ready from the server into this variable from the server, and then you slip it into your state
https://ssr.vuejs.org/ru/guide/data.html

A
Alex, 2019-01-22
@Kozack Vue.js

Well, you usually initialize the data by default, perhaps add a preloader state, and when your data from the database (or from somewhere else) comes, you replace

{
  template: `
    <div>
      <preloader v-if="!pageData" />
      <div v-else >
        ...
      </div>
    </div>
  `,

  data() {
    return {
      pageData: null
    }
  },

  async mounted() {
    this.pageData = await getData()
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question