GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs
  • Tutorials
  • Playground
  • Reference
  • code
  • Lazy loading services
  • Injection context
  • injectAsync
  • ProviderToken
  • InjectAsyncOptions
  • DefaultExport

This view

  • Original page
  • New address

GeezerVison is showing angular.dev — injectAsync • Angular

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>;

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})
@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})
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.