It's actually quite simple:
- supply the token
- provide the service
- inject the service
Example:
// defining the token
const token = new InjectionToken<string>('token');
// defining a service that relies on the token
@Injectable()
export class MyService {
// injecting the token into the service
constructor(@Inject(token) private token: string) {}
getValue(): string {
return this.token;
}
}
@Component({
selector: 'my-app',
standalone: true,
providers: [
// supplying the token & service
{ provide: token, useValue: 'foobar'},
MyService,
],
template: ``,
})
export class App {
constructor(myService: MyService) {
// enjoy
console.log(myService.getValue());
}
}
bootstrapApplication(App);