Vue 3 Router Link

Posted on  by admin

Right now it is possible to dynamically add routes by calling router.addRoutes([/* routes */]). It would be nice if it was possible to also remove/replace routes on the fly. In my app user may define routes by himself (on the fly). I keep user configuration in the Vuex store, and when it is updated I want to completely replace whole router routes configuration (though replacing/removing single route would be also handful but I guess then it should also handle removing/replacing/adding child routes instead of only dealing with top-level routes, which would be quite complicated).

How to use router params with Vue Router

Step 5 — Navigating Between Routes

I imagine that after replacing routes router would check if current path matches any of the new routes and navigated to it. It could be for example method router.replaceRoutes([/* routes */]) or router.setRoutes (in addition to method router.addRoutes which already exists).

Step 4 — Creating Routes With Parameters

Please note that this situation can be handled without VueRouter by setting one global route /* that will match every possible path, performing route matching inside this global route component (based on dynamic config stored in Vuex store for example) and dynamic rendering of component(s) that matched current path.


However this is very ugly solution which looses all benefits of VueRouter, and handling child routes would be complicated. In fact I wanted to handle it by patching VueRouter manually, but it seems that createMatcher ( does not expose pathMap and nameMap so it is not possible modify routes configuration in other way than by calling router.addRoutes (I guess it's intentional :).

The author selected Open Sourcing Mental Illness to receive a donation as part of the Write for DOnations program. Vue.js is a popular JavaScript framework for creating user interfaces.

Created in 2014 by Evan You (formally of Google), Vue.js is often described as a combination of React and Angular, borrowing the prop-driven development of React and the templating power of Angular. This makes Vue an accessible framework for beginners to pick up, especially since it focuses on traditional HTML and CSS, rather than being a CSS-in-JS framework like React or relying on TypeScript (a superset of JavaScript) like Angular does.