Skip to content
Injection is a powerful inversion of control container that is widely used in the midway framework and brings good user experience.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.githooks/pre-commit
benchmark
docs
scripts
src
test
.autod.conf.js
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
package.json
tsconfig.json
tslint.json

README.md

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 >= 8.0.0 required.

Injection requires TypeScript >= 2.0 and the experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.

{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["es2017", "dom"],
    "types": ["reflect-metadata"],
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Getting Started

import {container, provide, inject} from 'injection';

@provide('userModel')
class UserModel {

}

@provide('userService')
class UserService {
  
  @inject()
  private userModel;
  
  async getUser(uid) {
    // TODO
  }
}


const container = new Container();
container.bind(UserService);
container.bind(UserModel);

const userService = await container.getAsync('userService'); 
const data = await userService.getUser(123);

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

License

MIT

You can’t perform that action at this time.