Vue Single page app Router, what happens with the components when we change route?
Asked Answered
D

4

5

Let's suppose I have a component called FirstPage, which is my default route, now FirstPage triggers an asynchronous call, with the help of an action of the vuex store, to be made each minute to a backend Api (it's triggered when the component is loaded as a route), now let's say I go to an about route that goes to an About component, is FirstPage still making the calls?

Edit:
I'm not developing an app with that yet, so I can't provide examples.
It's on my interest to know the behavior in these cases of the router, because whenever I change the route I would want to stop making the constant calls (as they won't be necessary).
The reason is that Depending on this I'd have to switch tooling for a project I have in mind.

Despair answered 12/10, 2018 at 8:7 Comment(0)
T
4

In general, a component's instance will be destroyed when you navigate away from it. However, there are two exceptions to this ..

  1. When you use routes with params. From the Vue Router docs

One thing to note when using routes with params is that when the user navigates from /user/foo to /user/bar, the same component instance will be reused. Since both routes render the same component, this is more efficient than destroying the old instance and then creating a new one. However, this also means that the lifecycle hooks of the component will not be called.

  1. When you wrap your router-view component within a keep-alive element. Since the <router-view> is essentially a dynamic component.

Generally Vue does a very good job of housekeeping and cleaning up after a component's instance when it gets destroyed. But sometimes you'll have to do some manual cleanup, especially if you use some kind of external library. This is usually handled in the beforeDestroy hook of an instance's lifecycle.

Tache answered 12/10, 2018 at 9:12 Comment(0)
S
3

In normal conditions, any logic/scripts/etc done at creation inside said component will be "purged" on the on destroy/close hooks (not only pertinent to vue but seen in lots of other tools), if there is a need to persist something then it should be in a higher scope (or other solution)

Splasher answered 11/6, 2021 at 13:24 Comment(0)
U
0

Any script written for the respective component only runs if the component is rendered in page. Once you go to about component replacing the previous component then previous script wont run.

Unlawful answered 12/10, 2018 at 8:27 Comment(0)
P
0

you can make a parent component with a router-view and load in your page you always want to get loaded, so your FirstPage component, but this component should just have logic behind it, and no html because otherwise you will always see that rendered. Router-view the page you want to display the real html and stuff. I hope you get the idea, if not i could make an example for you. Goodluck.

Plemmons answered 12/10, 2018 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.