Learn the coding skills for your next job

How to Rerender a Vue Route When Path Parameters Change

In single-page apps that use the Vue Router, it’s common to create a path parameter that changes the behavior of a route. Often a problem occurs however when a user alters the path manually in the address bar. Manually changing the URL does not rerender the view! This can cause unexpected behavior because mounted() hooks don’t fire and nested components don’t reload.

How to Fix It

The solution is to use another Vue hook, beforeRouteUpdate(). Let’s take the example of the Qvault Playground. The last parameter in the Playground’s path is the code language, “js” or “go”. If the boilerplate code were only fetched using a mounted() hook, then when a user changed the path parameter the boilerplate code wouldn’t reload.

The reason that it does reload is that the Qvault SPA also has the following beforeRouteUpdate() hook:

beforeRouteUpdate (to, from, next) { this.lang = to.params.lang; this.setCode(); next(); }
Code language: JavaScript (javascript)

According to the docs, the hook receives three parameters:

  • to: the target Route Object being navigated to.
  • from: the current route being navigated away from.
  • next: this function must be called to resolve the hook. The action depends on the arguments provided to next:
    • next(): move on to the next hook in the pipeline. If no hooks are left, the navigation is confirmed.
    • next(false): abort the current navigation. If the browser URL was changed (either manually by the user or via the back button), it will be reset to that of the from route.
    • next('/') or next({ path: '/' }): redirect to a different location. The current navigation will be aborted and a new one will be started. You can pass any location object to next, which allows you to specify options like replace: truename: 'home' and any option used in router-link‘s to prop or router.push
    • next(error): (2.4.0+) if the argument passed to next is an instance of Error, the navigation will be aborted and the error will be passed to callbacks registered via router.onError().

In the case of the Qvault Playground, we are just doing the same operation that the mounted() hook does: we check the language and fetch the boiler plate.

What If I Want All Routes To Update?

If this is a common problem in your app, you can set your entire router-view to re-render when its path changes by providing a key property:

<router-view :key="$route.fullPath" />
Code language: HTML, XML (xml)

With the above, you won’t need to use the beforeRouteUpdate() hook, and can directly access the now-reactive this.$route.params.myVar property. The only problem with this method is that every path in that router will update in the case of a path change. You may not want all that needless re-rendering, but that’s a decision for you to make.

Trying to find your next programming job?

If you are a self-taught developer having trouble finding your first programming job, we've got your back! We have the learning resources and tight-knit dev community that you need to land the coding job you've been looking for. To get started, create a free account and join our Discord community.

Have questions or feedback?

If we've made a mistake in the article, please let us know so we can get it corrected!

Leave a Comment