Service

A long-lived singleton for shared state and logic across the app.

Since Ember 4/5 (Octane) Spec ↗

Syntax

class X extends Service {}

Returns

Service — A singleton injectable class.

Examples

import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';

export default class SessionService extends Service {
  @tracked currentUser = null;

  get isAuthenticated() {
    return this.currentUser !== null;
  }

  login(user) {
    this.currentUser = user;
  }
}
import Component from '@glimmer/component';
import { service } from '@ember/service';

export default class HeaderComponent extends Component {
  @service session;
}

Notes

Services are instantiated lazily and live for the app's lifetime. Inject them with the @service decorator into components, routes, controllers, or other services for cross-cutting concerns like auth, config, or stores.