Understanding the rationale for rendering Views with vuex dependencies in vueJS

I'm facing an issue with my API call in App.vue that populates the vuex store's state. The Home.vue component displays images based on the store's state, but console errors are thrown before the state is populated. I'm unsure about the best approach to delay loading views until the data they rely on is fully loaded. Most solutions seem tailored for components rather than router-views, and passing data through params doesn't seem practical in this scenario. As a beginner, I could use some guidance.

//App.vue
<template>
  <div id="app">
    <div id="nav">
        <div id="nav-logo"></div>
        <div id="nav-links">
          <router-link to="/">Home</router-link> 
          <router-link to="/pokemon">Pokemon</router-link> 
        </div>
    </div>

    <router-view></router-view>

  </div>
</template>

<script>
export default {
  created(){
    this.init();
  },
  methods:{
    init(){
      this.$store.dispatch('fetchPokemon', 'gen1');
    }
  }
}
</script>

//Home.vue
<template>
<div>
        <div id="pokemonAFront">
          <img :src="pokeImg[randomA].sprites.front_default" alt="">
        </div>

        <div id="pokemonABack">
          <img :src="pokeImg[randomA].sprites.back_default" alt="">
        </div>
</div>
</template>

<script>
export default {
  data(){
    return{
      randomA: Math.floor(Math.random()* 20),
   },
  computed:{
    pokeImg(){
      return this.$store.state.pokemon
    }
}
</script>
//store/index.js
state:{
   pokemon:[]
},
mutations: {
  SET_POKEMON(state, pokemon){

      state.pokemon = pokemon;
    }
},
actions: {
    fetchPokemon(context, currentGen){
      context.commit('SET_POKEMON', pokeData.fetchPokemon(currentGen)) //api call
     }
}

What approach should be taken to properly delay loading views/components until their vuex dependencies have finished loading?

Answer №1

To keep it simple, the DOM will only be rendered once the pokeImg data has been retrieved from the API:

 <div v-if="pokeImg.length > 0">
    <div id="pokemonAFront">
      <img :src="pokeImg[randomA].sprites.front_default" alt="">
    </div>

    <div id="pokemonABack">
      <img :src="pokeImg[randomA].sprites.back_default" alt="">
    </div>
 </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Styling a Pie or Doughnut Chart with CSS

I am working on creating a doughnut chart with rounded segments using Recharts, and I want it to end up looking similar to this: Although I have come close to achieving the desired result, I am encountering some issues: Firstly, the first segment is over ...

Using jQuery to target form elements that have a specific class assigned to them

I am facing an issue with targeting input fields within a specific form on a page. The form has a certain class, let's say "has-error," and I want to remove this class from all the input fields in that form. I attempted the following code snippet: t ...

Vue - Dynamically rendering a string as an element

Is there a way to generate a vue component using a string stored in my database? For instance, the string includes a message with a smiling emoji. I store it like this: Some text with Emoji: :santa::skin-tone-3:, and then replace all valid strings between ...

I'm having trouble getting my HTML POST request form to connect with the Express app.post. Any tips on how to properly pass on numeric variables to a different POST request?

There seems to be a misunderstanding or error on my part regarding POST and GET requests based on what I've read online. On myNumber.ejs, I have a submit form. Upon submission, the view switches to Add.ejs. The goal is for Add.ejs to display both the ...

Preventing page reload by using event.preventDefault() does not work with Ajax Form

I've been working on a code snippet to submit a form using Ajax without any page reloads: $( document ).on('submit', '.login_form', function( event ){ event.preventDefault(); var $this = $(this); $.ajax({ data: ...

The JSON.stringify function encounters difficulties when trying to format an object into

Why does JSON.stringify refuse to format objects and keep everything on a single line? <!DOCTYPE html> <html> <head> <script> var obj = { "a" : "1547645674576457645", "b" : "2790780987908790879087908790879087098", "c" ...

Executing the stop button in the browser with Webdriver IO

Just a bit of background: The program I'm testing is located within another website that requires login credentials. Once logged in, I can access the screen I need to test by typing in the direct link. However, there's an issue where my automated ...

Retrieve the HTTP Code and Response Header for a successful AJAX request

I am attempting to retrieve the HTTP Response Code/Response Header from my AJAX request. Below is the initial script I used: $("#callContact1").click(function() { $.ajax({ url: "https://www.server.com?type=makecall", data: {}, type: ...

Enhancing JavaScript Objects with New Key/Value Pairs

I'm trying to wrap my head around the code structure of a solution I stumbled upon. The issue at hand: Create a function that takes in a string with only lowercase letters as a parameter, and returns an object with each letter and the number of times ...

Display a 404 page on Vue router when the ID does not match

When it comes to displaying a 404 page if a parameter doesn't match, I'm a bit puzzled. For instance, if the user name is “joe” and someone tries to access “/joe/categories”, then the categories component should be displayed. Now, I want ...

Firebase Hosting integrated with Cloud Functions

Struggling with deploying my functions and hosting. While I have managed to get them working separately on different branches, integrating both hosting and cloud functions is proving to be a challenge. It seems like my cloud function is not deploying succ ...

Creating a local storage file named .notification.localstore.json specific to Microsoft ConversationBot

I am in need of utilizing a ConversationBot to send messages to MS Teams users. The code snippet below was obtained from an example app generated by TeamsToolkit. It functions as expected, however, it relies on user IDs stored in .notification.localstore.j ...

A guide on traversing through nested HTML divs using jQuery

I am facing an issue with nested HTML div elements on my website. The data is being fetched from a JSON file and stored in a variable. For example, the 'obj' variable contains an array of objects with properties such as Name, Progress, Descripti ...

utilize a Vue animation complete callback to incorporate a function

Can a function be passed inside the "done" callback for Vue animation? enter: function(e, done) { element.animate({ // ... }, duration, done) } Is there a way to ensure that the next "afterEnter" hook will still be called? If I wer ...

Tips on toggling between slides with Bootstrap Carousel

I am in the process of designing a portfolio website and have encountered a challenge. I want to divide it into two sections - a brief introduction and a carousel for showcasing projects. While I have managed to set up both sections, I'm facing an iss ...

Exploring the process of sending props via the render method in Vue.js and observing their behavior

Struggling with passing a prop from a parent component down to a child created using CreateElement/render in vue.js and then watching it. Here is the parent component: Vue.component('my-drawing', MyDrawing) new Vue({ el: '#drawing' ...

Guide to inserting an HTML file into an article's content

After downloading an extension from freefrontedit and uploading it to my server in the directory /accordeon, I successfully accessed it by pointing my browser to . However, I am now faced with the challenge of loading the index.html into my Joomla article ...

What is the best way to incorporate CDN into my webpack build process?

I have developed a module with the following code: export default () => console.log('hello my_module~!') The configuration in the webpack.config.js file looks like this: module.exports = { // ... output: { // ... library: 'he ...

Modifying an element's value according to user input: Step-by-step guide

Within my dropdown menu, there is a single option labeled "Others". Upon selecting this option, a textbox appears allowing me to input custom text. Is it possible to dynamically update the value of the <option value="Others">Others</option>, ...

The blur() function in -webkit-filter does not seem to function properly in Vue.js

I'm having trouble implementing the -webkit-filter: blur() property. I tried using "filter" instead, but it still isn't working. Could this be a limitation of Vue.js or are there any other solutions available? <template> <div class=&qu ...