import { Component, ReflectiveInjector } from '@angular/core'; import { MyService } from './my.service'; @Component({ selector: 'app-root', template: 'Hello World!
', providers: [MyService] }) export class AppComponent { constructor() { const injector = ReflectiveInjector.resolveAndCreate([MyService]); const myService = injector.get(MyService); myService.doSomething(); } }
import { Component, ReflectiveInjector } from '@angular/core'; import { AppConfig } from './app.config'; @Component({ selector: 'app-root', template: 'In this example, we are providing a configuration object using the provide token and useValue property. We then retrieve the configuration instance using the get() method and log its apiUrl property to the console. The @angular/core package library contains the ReflectiveInjector class and the get() method.Hello World!
', providers: [{ provide: AppConfig, useValue: { apiUrl: 'http://localhost:3000' } }] }) export class AppComponent { constructor() { const injector = ReflectiveInjector.resolveAndCreate([{ provide: AppConfig, useValue: { apiUrl: 'http://localhost:3000' } }]); const config = injector.get(AppConfig); console.log(config.apiUrl); } }