Skip to Content
Reactant
reactant-diModules

decorators/lazy

Functions

getLazyDecorator

▸ getLazyDecorator(getService): (serviceIdentifier: ServiceIdentifier<unknown>, enableCache: boolean) => (target: object, key: string | symbol) => any

Description

You can get a decorator @lazy(serviceIdentifier) with getLazyDecorator((serviceIdentifier) => container.get(serviceIdentifier)), and use it on any one dependency property that you need to lazily get.

Example

let container: Container;
const lazy = getLazyDecorator((serviceIdentifier) =>
  container.get(serviceIdentifier)
);

@injectable()
class Foo {
  public get test() {
    return 'test';
  }
}

@injectable()
class Bar {
  @lazy('foo')
  foo?: Foo;
}

container = createContainer({
  ServiceIdentifiers: new Map(),
});

const bar = container.get(Bar);

container.bind('foo').to(Foo);
expect(bar.foo?.test).toBe('test');

Parameters

NameType
getService(serviceIdentifier: ServiceIdentifier<unknown>, target?: object) => unknown

Returns

fn

▸ (serviceIdentifier, enableCache?): (target: object, key: string | symbol) => any

Parameters
NameTypeDefault value
serviceIdentifierServiceIdentifier<unknown>undefined
enableCachebooleantrue
Returns

fn

▸ (target, key): any

Parameters
NameType
targetobject
keystring | symbol
Returns

any

Defined in

packages/reactant-di/src/decorators/lazy.ts:43

On this page