How would I get a list of all the routes of my application in Ember.js 3.18?

Viewed 212

I am trying to build an automatic navigation.

How would I get a list of all the routes of my application in Ember.js 3.18?

This question was asked 4 years ago and the answers seem outdated and hack-ish.

I have been trying several things. This kind of code gets me the current route name. But that's not what I want: I want an object of all the available routes.

In component JS:

import { inject as service } from '@ember/service';

export default class navComponent extends Component {
  @service router;
}

In template:

{{router.currentRouteName}}

1 Answers

As far as I know, there's no public API for that. However, you can get what you want through a private API:

this.router._router._routerMicrolib.recognizer.names

This returns an object where the keys are the routes in your application. It also adds _loading and _error routes so you might want to do some filtering, depending on your needs.

If you decide to use this, I would advise to use a test! Private APIs can change when a new version of Ember.js comes out so a test will prevent you from having a bad surprise after an update.

Related