Function

Module: @ember/service

Available since v4.1.0

name
String
(optional) name of the service to inject, defaults to the property's name
returns
ComputedDecorator
injection decorator instance

Creates a property that lazily looks up a service in the container. There are no restrictions as to what objects a service can be injected into.

Example:

app/routes/application.js
1
2
3
4
5
6
7
8
9
10
import Route from '@ember/routing/route';
import { service } from '@ember/service';

export default class ApplicationRoute extends Route {
  @service('auth') authManager;

  model() {
    return this.authManager.findCurrentUser();
  }
}

Classic Class Example:

app/routes/application.js
1
2
3
4
5
6
7
8
9
10
import Route from '@ember/routing/route';
import { service } from '@ember/service';

export default Route.extend({
  authManager: service('auth'),

  model() {
    return this.get('authManager').findCurrentUser();
  }
});

This example will create an authManager property on the application route that looks up the auth service in the container, making it easily accessible in the model hook.