Skip to main content

Module: decorators/inject

Functions

inject

inject(serviceIdentifierOrFunc?): (target: object, key?: string, index?: number) => void

Description

You can use @inject() to perform the required dependency injection module to decorate in the constructor of an injectable class.

If the default is a dependency injection of the class itself as a type, e.g. @inject(Foo) foo: Foo, then it is exactly the same as foo: Foo.

Example

@injectable()
class Bar {
getValue() {
return 'bar';
}
}

@injectable()
class Foo {
getValue() {
return 'foo';
}
}

@injectable()
class FooBar {
constructor(@inject() public bar: Bar, @inject('foo') public foo: Foo) {}
}

const fooBar = testBed({
modules: [
Bar,
{ provide: 'foo', useClass: Foo },
],
main: FooBar,
});

expect(fooBar.instance.foo.getValue()).toBe('foo');

Parameters

NameType
serviceIdentifierOrFunc?ServiceIdentifierOrFunc<any>

Returns

fn

▸ (target, key?, index?): void

Parameters
NameType
targetobject
key?string
index?number
Returns

void

Defined in

packages/reactant-di/src/decorators/inject.ts:51