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
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.

Show:

Defines the properties that will be concatenated from the superclass (instead of overridden).

By default, when you extend an Ember class a property defined in the subclass overrides a property with the same name that is defined in the superclass. However, there are some cases where it is preferable to build up a property's value by combining the superclass' property value with the subclass' value. An example of this in use within Ember is the classNames property of Component from @ember/component.

Here is some sample code showing the difference between a concatenated property and a normal one:

import EmberObject from '@ember/object';

const Bar = EmberObject.extend({
  // Configure which properties to concatenate
  concatenatedProperties: ['concatenatedProperty'],

  someNonConcatenatedProperty: ['bar'],
  concatenatedProperty: ['bar']
});

const FooBar = Bar.extend({
  someNonConcatenatedProperty: ['foo'],
  concatenatedProperty: ['foo']
});

let fooBar = FooBar.create();
fooBar.get('someNonConcatenatedProperty'); // ['foo']
fooBar.get('concatenatedProperty'); // ['bar', 'foo']

This behavior extends to object creation as well. Continuing the above example:

let fooBar = FooBar.create({
  someNonConcatenatedProperty: ['baz'],
  concatenatedProperty: ['baz']
})
fooBar.get('someNonConcatenatedProperty'); // ['baz']
fooBar.get('concatenatedProperty'); // ['bar', 'foo', 'baz']

Adding a single property that is not an array will just add it in the array:

let fooBar = FooBar.create({
  concatenatedProperty: 'baz'
})
view.get('concatenatedProperty'); // ['bar', 'foo', 'baz']

Using the concatenatedProperties property, we can tell Ember to mix the content of the properties.

In Component the classNames, classNameBindings and attributeBindings properties are concatenated.

This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently. Since it changes expectations about behavior of properties, you should properly document its usage in each individual concatenated property (to not mislead your users to think they can override the property in a subclass).

The currentRoute property contains metadata about the current leaf route. It returns a RouteInfo object that has information like the route name, params, query params and more.

See RouteInfo for more info.

This property is guaranteed to change whenever a route transition happens (even when that transition only changes parameters and doesn't change the active route).

Usage example:

app/components/header.js
  import Component from '@glimmer/component';
  import { service } from '@ember/service';
  import { notEmpty } from '@ember/object/computed';

  export default class extends Component {
    @service router;

    @notEmpty('router.currentRoute.child') isChildRoute;
  });

Name of the current route.

This property represents the logical name of the route, which is dot separated. For the following router:

app/router.js
 Router.map(function() {
   this.route('about');
   this.route('blog', function () {
     this.route('post', { path: ':post_id' });
   });
 });

It will return:

  • index when you visit /
  • about when you visit /about
  • blog.index when you visit /blog
  • blog.post when you visit /blog/some-post-id

Current URL for the application.

This property represents the URL path for this route. For the following router:

app/router.js
 Router.map(function() {
   this.route('about');
   this.route('blog', function () {
     this.route('post', { path: ':post_id' });
   });
 });

It will return:

  • / when you visit /
  • /about when you visit /about
  • /blog when you visit /blog
  • /blog/some-post-id when you visit /blog/some-post-id

Destroyed object property flag.

if this property is true the observers and bindings were already removed by the effect of calling the destroy() method.

Destruction scheduled flag. The destroy() method has been called.

The object stays intact until the end of the run loop at which point the isDestroyed flag is set.

The location property returns what implementation of the location API your application is using, which determines what type of URL is being used.

See Location for more information.

To force a particular location API implementation to be used in your application you can set a location type on your config/environment. For example, to set the history type:

config/environment.js
'use strict';

module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'router-service',
    environment,
    rootURL: '/',
    locationType: 'history',
    ...
  }
}

The following location types are available by default: hash, history, none.

See HashLocation. See HistoryLocation. See NoneLocation.

Defines the properties that will be merged from the superclass (instead of overridden).

By default, when you extend an Ember class a property defined in the subclass overrides a property with the same name that is defined in the superclass. However, there are some cases where it is preferable to build up a property's value by merging the superclass property value with the subclass property's value. An example of this in use within Ember is the queryParams property of routes.

Here is some sample code showing the difference between a merged property and a normal one:

import EmberObject from '@ember/object';

const Bar = EmberObject.extend({
  // Configure which properties are to be merged
  mergedProperties: ['mergedProperty'],

  someNonMergedProperty: {
    nonMerged: 'superclass value of nonMerged'
  },
  mergedProperty: {
    page: { replace: false },
    limit: { replace: true }
  }
});

const FooBar = Bar.extend({
  someNonMergedProperty: {
    completelyNonMerged: 'subclass value of nonMerged'
  },
  mergedProperty: {
    limit: { replace: false }
  }
});

let fooBar = FooBar.create();

fooBar.get('someNonMergedProperty');
// => { completelyNonMerged: 'subclass value of nonMerged' }
//
// Note the entire object, including the nonMerged property of
// the superclass object, has been replaced

fooBar.get('mergedProperty');
// => {
//   page: {replace: false},
//   limit: {replace: false}
// }
//
// Note the page remains from the superclass, and the
// `limit` property's value of `false` has been merged from
// the subclass.

This behavior is not available during object create calls. It is only available at extend time.

In Route the queryParams property is merged.

This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently. Since it changes expectations about behavior of properties, you should properly document its usage in each individual merged property (to not mislead your users to think they can override the property in a subclass).

The rootURL property represents the URL of the root of the application, '/' by default. This prefix is assumed on all routes defined on this app.

If you change the rootURL in your environment configuration like so:

config/environment.js
'use strict';

module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'router-service',
    environment,
    rootURL: '/my-root',

  }
]

This property will return /my-root.