Skip to content

Provide method to get all routes #2940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
goeh opened this issue Sep 22, 2019 · 5 comments
Closed

Provide method to get all routes #2940

goeh opened this issue Sep 22, 2019 · 5 comments
Labels
feature request fixed on 4.x This issue has been already fixed on the v4 but exists in v3 group[dynamic routing] Issues regarding dynamic routing support (eg add, replace, remove routes dynamically)

Comments

@goeh
Copy link

goeh commented Sep 22, 2019

What problem does this feature solve?

If an application is split into several Vue components/libraries, each component can add their specific routes using router.addRoutes(array).
In the main application we then want to dynamically create menu items from all available routes. Currently there seems to be no way to get all routes, including those added with addRoutes().

We've done a workaround by importing routes manually from all included components and appending them into one array. This array is then used when constructing the router instance. It's a lot of boilerplate and easy to miss an import. A Router.getRoutes() method would really help in this use-case.

What does the proposed API look like?

router.getRoutes() or router.getAllRoutes() that returns an array of route configurations.

@yurii-github
Copy link

Yes, this is somewhat important data to get.
I did workaround for myself like

(function(f) {
  VueRouter.prototype._allRoutes = null
  VueRouter.prototype.allRoutes = function allRoutes () {
    return this._allRoutes
  }
  VueRouter.prototype.addRoutes = function addRoutes (routes = []) {
    if (this._allRoutes === null) { // init
      this._allRoutes = this.options.routes
    }
    routes.forEach(function(r) {
      // TODO: replace unique paths
      this._allRoutes.push(r)
    }.bind(this))
    return f.apply(this, arguments);
  }
})(VueRouter.prototype.addRoutes)

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {path: '/', component: require('./pages/Index.vue').default},
  ]
})

router.addRoutes([{path: '/about', component: require('./pages/About.vue').default},])
router.addRoutes([{path: '*', component: require('./pages/Error404.vue').default}])

console.log('ALL ROUTERS',router.allRoutes()) // 3 paths here

but it doesn't work for children and same path routes.

@hollote
Copy link

hollote commented Oct 29, 2019

It will be good to have them all in one place, as for example here: router.options.routes.
All defined routes and all added dynamically. Can we have it somehow?

@posva posva added the group[dynamic routing] Issues regarding dynamic routing support (eg add, replace, remove routes dynamically) label Feb 6, 2020
@iamzozo
Copy link

iamzozo commented Mar 4, 2020

I've tried a simple solution with returning the pathMap also from the matcher, and make a getter on main instance: getRoutes. What do you think?
iamzozo@94145b9

@posva
Copy link
Member

posva commented Mar 4, 2020

@iamnotblank thanks! This method is unlikely to be added to the current version of Vue Router to avoid adding more breaking changes 😅
It will be on the next version though, as part of the Dynamic Routing API (vuejs/rfcs#122)

@posva posva added the fixed on 4.x This issue has been already fixed on the v4 but exists in v3 label Mar 4, 2020
@posva posva removed the discussion label Dec 29, 2020
@posva
Copy link
Member

posva commented Jan 5, 2021

Added at 6bc30aa with limited functionality compared to v4

@posva posva closed this as completed Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request fixed on 4.x This issue has been already fixed on the v4 but exists in v3 group[dynamic routing] Issues regarding dynamic routing support (eg add, replace, remove routes dynamically)
Projects
None yet
Development

No branches or pull requests

5 participants