question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide method to get all routes

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
hollotecommented, 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?

2reactions
yurii-githubcommented, Oct 28, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router: get all routes as array
Another possible way to represent a route is to use JSX, which has a different structure itself, for example: childRoutes is represented as...
Read more >
URL Dispatch
The App::route() method provides simple way of registering routes. This method adds a single route to application routing table. This method accepts a...
Read more >
Routers - Django REST framework
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
The routes are defined either using .get() or .post() methods on the router object. All the paths are defined using strings (we don't...
Read more >
Spring Cloud Gateway
Spring Cloud Gateway aims to provide a simple, yet effective way to route to ... The Path Route Predicate Factory takes two parameters:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found