-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
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). 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).
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
(https://github.com/vuejs/vue-router/blob/dev/src/create-matcher.js#L16) 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 :).
Activity
posva commentedon Mar 9, 2017
alekbarszczewski commentedon Mar 9, 2017
In my app user may dynamically set routing. For example he has three predefined components A, B and C; In the application (in browser :) he can manage route configuration; let's say there is a textarea with following JSON:
For now I can just add new routes to the router:
What I need is to completely replace all routes:
router.replaceRoutes(routes)
or mayberouter.addRoutes(routes, { replace: true })
(it should completely replace routes configuration).#1129 is close but it won't solve my problem because I don't know route names...
I know that this is not primary use-case for VueRouter, but it would allow to do more advanced things (like my use-case).
ianaya89 commentedon Mar 17, 2017
I like this feature request, I have uses cases where I need to replace url parameters dynamically just to generate a URL to share and grant a unique access for a view with data that I already have in memory (where I don't need view changes or async operations).
nickforddev commentedon Apr 12, 2017
My application has different very routes for different user roles (some are replaced, some should be inaccessible or not exist at all). While it is already possible to achieve this using hooks and basic permissions definitions, I am interested in the idea of replacing the routes entirely on login/logout. The set of routes available could then be unique for each user role, and public/non-authenticated users.
overwriteNames
#1129fritx commentedon Jul 1, 2017
I had my
components
depends on theapi
, and theapi
now needs to depend on therouter
,however, the
router
has to depend on thecomponents
,because of the
new Router({ routes: [{ component }] })
syntax,which causes a circular require now.
(the
resolve => reuqire([path], resolve)
syntax doesn't work for me.)+1 for dynamic routes config, so I can export the router instance first.
feat(router): add 'replaceRoutes' method
nickforddev commentedon Sep 26, 2017
Is there any intention of implementing this? It felt like there was some momentum on this for a while
52 remaining items
aparajita commentedon Jun 8, 2020
renatodeleao commentedon Nov 12, 2020
Another usecase: oversimplifying a lot for demo purposes but let's say that I have basically 2 apps running while migrating a codebase:
legacy
(not-vue) tonext
(vue) and they both map the same urls for user convenience while sharing links in this transition period. Somyapp.com/some-page
will render either alegacy
app view or anext
app view based on a userfeatureFlag
(like a beta testing, but remember they use the same url!!) so picture an incremental migration.The problem is that switching accounts on the app (meaning no page reload) and assuming
curAccount
hadfeatureFlag === true
and we switch toaccount_b
has thefeatureFlag === false
, means that I need to removeunderTestingNextRoutes
from therouter
routes
array
so thataccount_b
effectively sees thelegacy
app view when navigating to that url.Does this make sense on why I can't simply use router hook/guard? In practice i want to redirect... to the same url.
Note that i'm not saying there isn't another way of doing it, and yes "you should just use a
next.myapp.com
" are expected (reddit) counter-arguments. I'm just trying to express what it seems to be a practical use-case for the feature.✌️ ☮️
redfox05 commentedon Nov 25, 2020
@posva It says you added
fixed in 4.x
but can you link to a commit/documentation so we can know how to use it and close this issue if it solves it?posva commentedon Nov 25, 2020
@redfox05 it's here: https://github.com/vuejs/router for Vue 3
redfox05 commentedon Nov 26, 2020
@posva Thanks, was hoping for a commit hash or link to documentation, but understand you're probably busy.
For others interested in the information, I managed to find the docs for it here: https://next.router.vuejs.org/guide/advanced/dynamic-routing.html#removing-routes
I was surprised this issue was not linked to a commit when the feature was implemented, and this issue is still Open, so figured it was not fully complete? Hence I tried looking for a commit hash but couldn't find one. Hopefully the docs will be enough for people.
[-]Feature request: replace routes dynamically[/-][+]Remove existing routes[/+]tomoat commentedon Apr 6, 2022
Only according to the name to delete the router now? How to delete the router by path?
dakt commentedon Mar 5, 2023
Really, how does one remove unnamed route?
gzusgo-thinkbig commentedon Jun 9, 2023
The reason this feature is no only a good idea but a must have, is because the nested routes behave very buggy, every time you go in and out and of the nested child of the route, and the route no even takes you where you want to go instead it take you to the last visited child of the parent.
This make navigation and routing very unpredictable and impossible to work with.
HongYangHT commentedon Apr 3, 2024
使用addRoute回调, 可以实现删除路由, 官网查看
const removeRoute = router.addRoute(routeRecord)
removeRoute() // 删除路由如果存在的话
trry-hub commentedon May 11, 2024
你这个是vue-router 4.x 的,但3.x对应的版本中没有这个方法