Package Exports
- injection
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (injection) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Injection
Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.
Installation
$ npm install injection reflect-metadata --saveNode.js >= 10.0.0 required.
Injection requires TypeScript >= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap":true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"pretty": true,
"declaration": true,
"outDir": "dist",
"lib": ["ES2018", "dom"]
}
}Getting Started
import {Container, provide, inject} from 'injection';
@provide('userModel')
class UserModel {
}
@provide('userService')
class UserService {
@inject()
private userModel;
async getUser(uid) {
// TODO
return 'Alex';
}
}
const container = new Container();
container.bind(UserService);
container.bind(UserModel);
async function getData() {
const userService = await container.getAsync<UserService>('userService');
const data = await userService.getUser(123);
return data;
}
getData().then(console.log);
// AlexDocument: https://midwayjs.org/injection/guide.html