Refreshing the child route page does not display the specific data assigned to that particular route

I have everything set up on codesandbox. Any assistance would be highly appreciated.

// Here is the link to my Axios Getter in /year

  methods: {
    async fetchData() {
      const self = this;
      const response = await axios.get(
        "https://example.com/data.json"
      );
      this.data = response.data;
      this.y2001Data = response.data.y2001;
      this.y2002Data = response.data.y2002;
      this.y2003Data = response.data.y2003;
      console.log(this.y2001Data, "this.y2001Data From Year.vue");
      console.log(this.y2002Data, "this.y2002Data From Year.vue");
      console.log(this.y2003Data, "this.y2003Data From Year.vue");
    }
  },

  mounted() {
    this.fetchData();
    console.log(this.data, "From Year.vue - Before Mount");
  }

I retrieve the data here and pass it down to the child routes. In /year/2001:

  beforeMount() {
    this.data = this.$parent.y2001Data;
    console.log(this.data, "From y2001Data.vue's Parent ");
  }

Then I pass this data to a child component in the child route.

<ChildComponent :data="this.data"></ChildComponent>

// Child Route that utilizes data from /Year Axios Call

When I navigate to the above child route (/year/2002), I do not see any data for 2002, but visiting 2001 and 2003 shows the data correctly.

If I refresh one of the other child routes (2001 or 2003), the data for 2002 appears and the refreshed route no longer displays any data.

However, if I refresh the main /Years route, all the data displays properly.

Any idea why this issue occurs and how to resolve it? I prefer direct linking to specific routes over redirection if possible.

Sandbox Editor: https://codesandbox.io/s/xyz123

Answer №1

The issue you are facing is due to the fact that an ajax call is only made when the year view is accessed. If you directly navigate to the 2002 view, there will be no ajax call and thus no data to populate the page. To resolve this, you should include an ajax call in the 2002 view so that data can be loaded even when accessing it directly. It seems like you are utilizing a PWA format, which I am not very familiar with. In my approach, I would designate the year.vue view as the main component and create a child component to handle data population for each specific year. This way, data can be passed down through props without encountering any issues. Additionally, consider using Arrow Syntax (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) to simplify your code and eliminate the need for const self = this;. It is also advisable to avoid loading data for multiple components all at once. While it may work fine in your current scenario, having a large amount of data could significantly slow down the initial load time. Instead, make individual calls for the required data and consider implementing caching if the data doesn't change frequently.

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

How to break out of an endless loop in Node.js and Express.Js

I have developed an Express app that is designed to paginate through an external API call. I have thoroughly examined the code but for some reason, the loop condition to break the function isn't being triggered. Any assistance on this matter would be ...

Working with attributes in AngularJS directives

Building an overlay (or modal window) in AngularJS has been on my mind, and I've made some progress with the html/css layout. Here's a sneak peek at what it looks like: <section class="calendar"> <a open-overlay="overlay-new-calenda ...

Is there a way to ensure that introjs follows the structure of slides effectively?

I created a modal that features multiple slides, each containing its own form. <v-bottom-sheet v-model="showCreateRecordModal" inset max-width="600px" class="rounded-t-xl" scrollable persistent> <v-card v-if=&quo ...

How can I determine in JavaScript if the Backspace key is being long pressed on iOS or Android devices?

In the process of developing a web application using Vue.js, I encountered an issue with detecting long presses of the Backspace key on desktop keyboards (Windows PCs specifically). On desktop, the event triggers multiple times as long as the Backspace key ...

How can I send an error message from a Laravel controller and handle it in InertiaJS using the form helper?

I am facing an issue where I need to handle errors in the controller, but they are not Laravel validation errors. This prevents me from accessing them using the inertiajs form helper without using props.errors. How can I return the error in a way that allo ...

Executing code within Vue js $set method's callback

For my v-for loop that generates a list of flights, I have implemented functionality where the user can click on a "flight details" button to send an AJAX request to the server and append new information to the results array. To update the view accordingly ...

Changing a 64-bit Steam ID to a 32-bit account ID

Is there a way to convert a 64-bit Steam ID to a 32-bit account ID in Node.js? According to Steam, you should take the first 32 bits of the number, but how exactly can this be done in Node? Would using BigNumber be necessary to handle the 64-bit integer? ...

When incorporating web animation into an SVG graphic, the animation may malfunction if used as a background

Recently, I created an SVG-based loading indicator using an online tool. However, every time a page with this loading indicator loads, Chrome displays a warning about SMIL animations being deprecated. To address this issue, I decided to explore replacing t ...

What causes a service called from external of angular to not act as a singleton?

Currently, I am in the process of working on a project that requires me to access a service from outside the Angular service. Unfortunately, it seems that the service retrieved from outside of Angular is not the same instance as the one inside the applicat ...

NextJS 13 causes tailwind to malfunction when route group is utilized

I've encountered an issue in my NextJS 13 application where Tailwind classes are no longer being applied after moving page.tsx/layout.tsx from the root directory to a (main) directory within the root. I suspect that there may be a configuration that i ...

How to include a javascript file in a vuejs2 project

Just starting out with the Vue.js framework and I've hit a snag trying to integrate js libraries into my project. Would greatly appreciate any assistance! By the way, I attempted adding the following code to my main.js file but it didn't have th ...

Personalized Facebook Share Dialog - unique title and description

I need to send a link using the FB Send Dialog with custom name and description. FB.ui({ method: 'send', name: 'Custom name', display: 'popup', link: 'http://link.com', to: facebookUserId, description: 'Custom ...

Remove the dropdown menu in real time, ensuring that only the most recent one is

Although the example may seem unusual, I am deliberately recreating a real problem to find a solution. When I click on a button, it generates 3 dynamic dropdowns based on a list of animals. My issue is that I want to delete a selected item, but it always ...

Ways to incorporate a while loop into a randomizing function

I've been diving into JavaScript and managed to create a website that displays random gifs when clicked from an array. Now, my goal is to implement a while loop to prevent duplicate images from being shown consecutively. However, I seem to be facing ...

html issue with d3.js rendering images: works in firefox, but not in chrome

I am currently experiencing an issue with a D3 graphic that runs successfully in Firefox but fails to render in Chrome when using an HTML and JSON file. Upon checking the console, I see the following error message: d3.v3.min.js:1 Access to XMLHttpReque ...

Unable to display JSON results in a tabular format

After successfully implementing the AJAX method in jQuery, I am able to receive a response. However, I am encountering difficulties when trying to display the arrays in a table format. success:function(resp){ var json =JSON.parse(JSON.stringif ...

Ways to stop a JavaScript function from running during page loading

After clicking the submit button on my form, I want to send a confirmation post using the sendPostAjax() function before submitting the form to the payment provider. However, I'm facing an issue where the sendPostAjax() function is getting executed as ...

Issue with jQuery .on('change') function not functioning correctly following a row being duplicated

Within my input form, I have implemented an on-click function that triggers an ajax call to submit a table row to the database. Upon submission, the row is then duplicated so that the user can input more data if necessary. The issue I am currently facing r ...

Discovering the minimum and maximum within an array containing objects

I am working with an array of objects that contain latitude and longitude points: var pts = [ { "X": 52.67528921580262, "Y": 8.373513221740723 }, { "X": 52.6759657545252, "Y": 8.374114036560059 }, { "X": 52.682574466310314, "Y": 8.372569084 ...

What is the importance of utilizing `document.createElementNS` when incorporating `svg` elements into an HTML webpage using JavaScript?

Not Working Example: const svg = document.createElement('svg') svg.setAttribute('height', '100') svg.setAttribute('width', '100') document.body.appendChild(svg) const rect = document.createElement(&apos ...