NullInjectorError при использовании inject() в runInContext

Следующий код срабатываетNullInjectorErrorкогда я звоню@inject()вrunInContext. Этого не происходит, если службаprovidedIn: 'root'или прошел в модулеproviders.

Любая идея, почему?

      @Injectable()
class Service {}

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [Service],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;

  public constructor(service: Service, injector: EnvironmentInjector) {
    console.log(service); // OK
    console.log(inject(Service)); //OK

    setTimeout(() => {
      injector.runInContext(() => {
        console.log(inject(Service)); //KO
      });
    }, 1000);
  }
}

Stackblitz

1 ответ

Согласно Angular#47566, в настоящее время это дизайн, но его можно улучшить.

Так что подождите и увидите.

Другие вопросы по тегам