Preserve router animations using :key attribute, while ensuring that parent views do not reload

My app utilizes :key="$route.path" in the router view for transitions, but this approach leads to the child route base view being refreshed and triggering certain APIs again.

Routes:

{
  path: "/some-route",
  component: () => import("@/views/SomeView/SomeViewBase.vue"),
  children: [
    {
      path: "view/:entity_id",
      name: "some-entity-details",
      component: () => import("@/views/SomeView/SomeEntityDetails.vue"),
    },
    {
      path: "update/:entity_id",
      name: "some-entity-update",
      component: () => import("@/views/SomeView/SomeEntityUpdate.vue"),
    },
  ]
}

SomeViewBase.vue:

<template>
  <div>
    <div class="flex justify-between items-center">
      // Some content here
    </div>

    <RouterView />
  </div>
</template>

<script setup lang="ts">
// Some logic here along with some API calls
</script>

The issue arises when switching between the child routes also causes a refresh of the base view. Although removing :key from App.vue resolved the problem, it compromised the animations.

I originally anticipated that utilizing child routes would only refresh the child route view if the route matches those specified in the children array.

Answer №1

Your request is causing a conflict.

On one side, you are asking Vue to treat each path component as a separate instance for animations, and on the other side, you want them to be the same instance for API calls.

You can't have it both ways – it's either separate instances or not.

To achieve both behaviors, I recommend using $route.path keys and moving API calls to a store or a higher-level component in the DOM hierarchy that includes <RouterView />, preventing re-execution when it changes.

If you need further assistance, consider creating a reproducible example on codesandbox.io with more detailed information about your app structure, route transitions, package versions, etc...

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

Troubleshooting a React Node.js Issue Related to API Integration

Recently, I started working on NodeJs and managed to create multiple APIs for my application. Everything was running smoothly until I encountered a strange issue - a new API that I added in the same file as the others is being called twice when accessed fr ...

Vuejs tab not displaying images

I have created a method called "numberMatch" that adds a gif, stored in a variable, to a tab when clicked. The method is functioning correctly, but when I try to display the gif on the page by calling the tab in the template, it only shows the path of th ...

Getting started with Babylon.js using npm: A step-by-step guide

I recently posted about my struggles with setting up Babylon.js using npm on Stack Overflow. Since I haven't received any answers yet, I was hoping to rephrase my question: Can someone provide a detailed step-by-step guide on how to set up Babylon.js ...

The JQuery TextNTags plugin eliminates tag formatting once the trigger syntax has been modified

I have incorporated the JQuery TextNTags plugin into my web application. Here is the original code snippet: $.browser = { webkit: true }; $(function () { $('textarea.tagged_text').textntags({ triggers: {'!': { ...

Revise the reply within ExpressJS

I need help with editing the response to a request in Express. When the request is made via XHR, I want to encapsulate the body inside a JavaScript object. This way, different parts of the page can be accessed individually within the JavaScript object, suc ...

Trouble with Sound during Quickblox Webrtc Audio Calls

I am having an issue with my audio calls. When I make a call to another user, everything seems fine except that I cannot hear any sound when speaking into the microphone. I am only interested in making audio calls. The call is initiated by pressing a butt ...

Tips for creating a unit test for a React Component utilizing the useEffect hook and asynchronous fetch

Starting from a basic create-react-app setup, I am diving into the world of unit testing to enhance my skills. The goal is to update the main App component to fetch data from a local node API and display it as a string response. However, I'm encounter ...

Is there a way to make this eval() function function properly in Internet Explorer?

My JavaScript code is fetching another JavaScript "class" from a different XHTML page. The fetched JavaScript looks like this: (function() { this.init = function() { jQuery("#__BALLOONS__tabs").tabs(); }; }) Once the f ...

Retrieving the image source from the image element by utilizing $(this).find("");

Currently facing a challenge in retrieving the image source (e.g., ./imgs/image.jpg) from an image element. Managed to make some progress by using the following code: var image = document.getElementById("home-our-doughnuts-box-image").getAttribute("src" ...

Transforming Unicode escape sequences into symbols and displaying them in a DOM element

Using the latest versions of Firefox and Chrome with jQuery 1.x edge. When an ajax request returns a single line of minified JSON text like this: { "fromSymbol": "\\u04b0", "toCurrency": "AUD", "toSymbol": "\\u0024", "convFact ...

Is it possible to cache an html page and display it on the current page when reloading?

I have been attempting to design a webpage with links in a table that trigger modals when clicked. These links are added dynamically using Jquery. $("table").append('<tr><td><a href="#exampleModal" data-bs-t ...

Is it possible to enable a button as soon as input is entered?

I'm encountering a minor issue with my button's functionality. I am attempting to have the button enabled as soon as text input is entered into the text field, but currently it only becomes enabled after the focus has changed from the text field. ...

Using two modal popups while passing an identifier

UPDATE: In my investigation, I discovered that a plain input tag without MVC RAZOR works as expected: <input type="text" class="hiddenid2" /> //WORKED However, when using the following code, it does not work: @Html.Editor("id", "", new { htmlAtt ...

What is the best way to collapse a button in asp.net using javascript after setting it to hidden?

I have a scenario where I have 3 buttons in a row on my asp.net page. Using JavaScript, I am setting the middle button (ButtonSR) to invisible. However, I want the button below it to move up and take its place instead of leaving an empty space. ...

Executing functionality based on changes in Angular bindings

I am working on securing user passwords in my HTML form. Currently, the password field is stored in localstorage like this: In JS: $scope.localStorage = localStorage; And then in HTML: <input id="pass" type="password" ng-model="localStorage.pass" re ...

Sending the ID of a mapped button to a component

I need help with my React mapping function that displays a series of cards, each with its own button to open up a dialog box. Right now, I'm struggling to pass the unique ID from each object to the correct dialog box. Instead, all IDs are being passed ...

Expanding the capabilities of search and replace in Javascript is imperative for enhancing its

I have developed a search and replace function. How can I enhance it by adding a comment or alert to describe the pattern and incorporating a functional input box? Any suggestions are welcome! <html> <head> <title> Search & Replace ...

What is the best approach to removing a component by utilizing the children prop in React?

I am currently working on a specific scenario: In my project, I have a component called Foo with a property named bar If the bar property is set to true, then I need to display the Bar component that is nested inside the Foo component If the bar prop ...

What is the method for inserting a line break into a string that is being transferred to a component in Next JS?

I am currently collaborating on a group project that involves developing a website. One of my team members created a ContentBlock component to facilitate the creation of new pages (see component code below). I have been working on a new page for the site a ...

What is the best method for choosing the parent elements?

I am attempting to create a sidebar that saves the last clicked link in local storage and still displays the collapsed links after the page is reloaded. $(".clickedLink").parent().parent().css('background-color', 'green'); Ca ...