Vue Router lifecycle: onRouteUpdateDuplicate
#483
Mini-ghost
started this conversation in
RFC Discussions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
In the common multi-page websites, the default behavior when we click on any hyperlink is to reload even if it is the same as current one.
But the same scenario in the single-page application, Vue Router will not to reload the page when the user clicks on the hyperlink that point to the same path. This makes sense, but sometimes we still need to reload some data on this page.
This RCF provides a solution where we can do something when routes are push or redirect to duplicate routes.
Basic example
In the example below, this page has two APIs, one to fetch advertisement and the other to fetch a list of products.
When the user clicks a link on this page, it happens that the path of the link point is the same as the current page, and the application can capture it to refresh the product data.
Motivation
Many scenarios require such functionality, such as Facebook or Twitter, when we click on a link from the same page, the application will not do nothing, it will update the timeline data to get the latest post.
In order to do this function more conveniently, we may need this lifecycle:
onRouteUpdateDuplicate()
Detailed design
Composition API
Registers a callback to be called when pushes or redirects to duplicate route.
Type
Details
The following situations are considered as route duplication
router.push()
orrouter.replace()
, but the specified route is completely consistent with the current path after parsing.Example
Options API
Call when the router is navigating to duplicate route.
Type
Example
Drawbacks
N/A
Alternatives
Naming
Variables could be named differently and proposals are welcome.
Other solutions
Before this solution is implemented, we still has the other way to solve this problem, but it is a little bit annoying.
We can create a
<CustomLink>
based hook system that depends on a library likeunjs/hookable
ordevelopit/mitt
, here is a quick demo.<CustomLink>
:And in page component:
Adoption strategy
This feature is additive and doesn't affect existing usage.
Unresolved questions
N/A
Beta Was this translation helpful? Give feedback.
All reactions