Answer the question
In order to leave comments, you need to log in
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
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
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 questionAsk a Question
731 491 924 answers to any question