Blog>
Snippets

Basic Injection Token Creation

Demonstrate how to create a lightweight injection token for Angular services using the InjectionToken class.
import { InjectionToken } from '@angular/core';

// Define a new injection token for a dependency
export const MY_SERVICE_TOKEN = new InjectionToken<MyService>('MY_SERVICE_TOKEN');
This code creates a new InjectionToken for a service named 'MyService'. This token can be used to inject the service into components or other services within the Angular application.