Spec-Zone.ru › Angular

injectAsync

A helper function that allows to inject dependencies asynchronously, which can be useful in cases when the dependency is not needed immediately and can be loaded lazily.

Lazy loading servicesInjection context

API

function injectAsync<T>(
  loader: () => Promise<ProviderToken<T>>,
  options?: InjectAsyncOptions | undefined,
): () => Promise<T>;
function injectAsync<T>(
  loader: () => Promise<DefaultExport<ProviderToken<T>>>,
  options?: InjectAsyncOptions | undefined,
): () => Promise<T>;
function injectAsync<T>(loader: () => Promise<ProviderToken<T>>, options?: InjectAsyncOptions | undefined): () => Promise<T>;

A helper function that allows to inject dependencies asynchronously, which can be useful in cases when the dependency is not needed immediately and can be loaded lazily.

NOTE: To enable lazy loading, the injected service must be auto-provided. This means it should be decorated with either @Injectable({providedIn: 'root'}) or @Service().

@paramloader() => Promise<ProviderToken<T>>

A function that returns a promise resolving to the injectable service

@paramoptionsInjectAsyncOptions | undefined

Configuration options for the async injection

@returns() => Promise<T>

A function that returns a promise resolving to the requested service instance.

Usage notes
class MyCmp {
 someSvc = injectAsync(() => import('..'));

 async onClick() {
   (await this.someSvc()).handleClick();
 }
}

// we can also configure prefetching:
injectAsync(.., {prefetch: onIdle})
function injectAsync<T>(loader: () => Promise<DefaultExport<ProviderToken<T>>>, options?: InjectAsyncOptions | undefined): () => Promise<T>;
@paramloader() => Promise<DefaultExport<ProviderToken<T>>>
@paramoptionsInjectAsyncOptions | undefined
@returns() => Promise<T>

Description

A helper function that allows to inject dependencies asynchronously, which can be useful in cases when the dependency is not needed immediately and can be loaded lazily.

NOTE: To enable lazy loading, the injected service must be auto-provided. This means it should be decorated with either @Injectable({providedIn: 'root'}) or @Service().

Usage Notes

class MyCmp {
 someSvc = injectAsync(() => import('..'));

 async onClick() {
   (await this.someSvc()).handleClick();
 }
}

// we can also configure prefetching:
injectAsync(.., {prefetch: onIdle})

Super-powered by Google ©2010–2025.
Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0.
https://angular.dev/api/core/injectAsync

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API