Class RouterService

public

The Router service is the public API that provides access to the router.

The immediate benefit of the Router service is that you can inject it into components, giving them a friendly way to initiate transitions and ask questions about the current global router state.

In this example, the Router service is injected into a component to initiate a transition to a dedicated route:

app/components/example.js
1
2
3
4
5
6
7
8
9
10
11
12
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';

export default class ExampleComponent extends Component {
  @service router;

  @action
  next() {
    this.router.transitionTo('other.route');
  }
}

Like any service, it can also be injected into helpers, routes, etc.