<!DOCTYPE html>
<html>
<head>
<link
href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
rel="stylesheet"
/>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99efecfcedf0ffe0d9a8b7e1">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"
/>
</head>
<body>
<div id="app">
<v-app>
<v-content>
<v-container>
<h2 class="red pa-2 primary white--text">Only Limited resolved questions</h2>
<section v-for="(item, index) in questions" :key="index">
<li>{{index}}.- {{item }}</li>
</section>
<section>
<h3>Response question 1</h3>
<blockquote class="pa-3 secondary white--text">
This can be done from any lifecycle hook, or a method. For the example, beforeCreate is used.
</blockquote>
<pre>
{{ $store.state.episode }}
</pre>
</section>
<section>
<h3>Response question 2</h3>
<blockquote class="pa-3 secondary white--text">This via getter and computed property</blockquote>
<div v-for="(item, i) in charactersInEpisode" :key="i">{{ item }}</div>
</section>
</v-container>
</v-content>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8cecdddf88a968e96898c">[email protected]</a>/dist/vue.js"></script>
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d3d0c0dde5978b958b95">[email protected]</a>"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9afacbcadb0bfa099e8f7a1">[email protected]</a>/dist/vuetify.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
[ Custom script excluded for brevity ]
</script>
</body>
</html>
I focus solely on solving the specified problem (resolved questions), omitting other irrelevant code due to undefined variables not provided in the context.
Response to question 1: How can I populate store state?
This task can be accomplished using any lifecycle hook or method. In this case, beforeCreate is utilized as an example.
async beforeCreate() {
const episode_id = 1; // TODO: Use ID for simplicity initially, then consider utilizing/adjusting params: this.$route.params.episode_id
await this.$store.dispatch("getEpisodeFromApi", episode_id);
},
Response to question 2: Is it possible to retrieve data from the store faster than the mounted hook execution?
In the Nuxt framework (built on Vue), this can be achieved through asyncData, which fetches data prior to rendering. With just Vue, control can be attained through getters and computed properties, among other methods; I propose using the latter to execute code efficiently.
// store/index.js
getters: {
charactersInEpisode: (state) => {
console.log("EXE GETTER", state);
if (state.episode) {
return state.episode.characters;
} else {
return [];
}
},
},
// Component.vue
computed: {
...Vuex.mapGetters(["charactersInEpisode"]),
},
NOTE:
setEpisode(state, payload) {
state.episode = payload[0]; // POTENTIAL ENHANCEMENT FROM BACKEND, SENDING OBJECT {} (CURRENTLY AN ARRAY [])
},
You can implement the solution and observe the reactivity. Hopefully, this proves beneficial.