Vue-router: Routing the same component, the component is not reload, but be reused?

Created on 4 Jun 2017  路  8Comments  路  Source: vuejs/vue-router

Version

2.5.3

Reproduction link

http://jsfiddle.net/L7hscd8h/1526/

Steps to reproduce

Please look at the demo

What is expected?

Reinitializes the components, the implementation of the component complete life cycle

What is actually happening?

The second route to components, components do not reload, lead to the previous page of the data has not been cleared

Most helpful comment

Here is a new version of your fiddle, using beforeRouteUpdate to re-initialize the data.

https://jsfiddle.net/Linusborg/L7hscd8h/1527/

Another possible way is to use a keyon the router-view to force the component to be re-created:

https://jsfiddle.net/Linusborg/L7hscd8h/1528/

All 8 comments

This is expected behaviour, Vue re-uses components where possible.

You can use the beforeRouteUpdatehook to react to a route switch that uses the same component.

Yes, although I also hope that the component be reused, but I more hope that data can be initialized again, otherwise the component does not make sense to me, as if it couldn't be continuous reuse.

I can only manual initialization data in beforeRouteUpdate? Seems it doesn't solve my problem.

I have to make the jump to a empty page, and then jump back, force the router to reload the components.

Is there a parameter can force it not be reused, but let it reload?

If it is the same path, and the same parameters, then reuse: no problem.

If it is the same path, but different parameters, then reuse is meaningless.

As a developer, really want is code reuse components, instead of reusing component data!

Change in the parameters, means that the component data needs to be initialized again!

Here is a new version of your fiddle, using beforeRouteUpdate to re-initialize the data.

https://jsfiddle.net/Linusborg/L7hscd8h/1527/

Another possible way is to use a keyon the router-view to force the component to be re-created:

https://jsfiddle.net/Linusborg/L7hscd8h/1528/

Thank you, : key can solve my problem.
Why in the document have not mentioned?

@LinusBorg is it somehow possible for the component not to be recreated, but catch every route update? Even in your example, beforeRouteUpdate is called only once, on the first visit versus the desired outcome - being called on any route/path/parameter change.

My code example was buggy, didn't call next:

https://jsfiddle.net/Linusborg/L7hscd8h/2208/

You could also use the props feature to use props instead of access ing the $route and beforeUpdate:

https://router.vuejs.org/en/essentials/passing-props.html

Please use forum.vuejs.org for further questions, not closed issues. Thanks :)

Was this page helpful?
0 / 5 - 0 ratings