Skip to main content

Function: Inject

Inject(serviceIdentifier): (target: DecoratorTarget<unknown>, targetKey?: string | symbol, indexOrPropertyDescriptor?: number | TypedPropertyDescriptor<any>) => void

Decorator on a dependency that specifies the identifier for that dependency.

Example

Inject MyService into MyComponent using the identifier (token) "ServiceIdentifier".

interface Service {
doSomething(): void;
}

@Injectable()
class MyService implements Service {
doSomething() {
console.log("Hello World!");
}
}

@MnComponent({
providers: [
{ provide: "ServiceIdentifier", useClass: MyService }
]
})
export class MyComponent {
// Search for a dependency with the identifier "ServiceIdentifier" and inject it into this property.
@Inject("ServiceIdentifier")
myService: Service;
}

Parameters

NameTypeDescription
serviceIdentifieranyIdentifier of the dependency.

Returns

fn

▸ (target, targetKey?, indexOrPropertyDescriptor?): void

Parameters
NameType
targetDecoratorTarget<unknown>
targetKey?string | symbol
indexOrPropertyDescriptor?number | TypedPropertyDescriptor<any>
Returns

void

Defined in

packages/core/src/core/di/inject.ts:35