injection
TypeScript icon, indicating that this package has built-in type declarations

1.8.6 • Public • Published

Injection

GitHub license GitHub tag Build Status Test Coverage Package Quality PRs Welcome

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

Node.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);
// Alex

Document: https://midwayjs.org/injection/guide.html

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i injection

Weekly Downloads

66

Version

1.8.6

License

MIT

Unpacked Size

422 kB

Total Files

113

Last publish

Collaborators

  • czy88840616
  • kurten