Skip to content

Dependency injection based on typescript+reflect-metadata

License

Notifications You must be signed in to change notification settings

guo-jianqiang/mini-di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini-di

Dependency injection based on typescript+reflect-metadata

install

yarn add @rainbow_deer/mini-di
npm install @rainbow_deer/mini-di

peerDependencies

yarn add reflect-metadata

babel

yarn add @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties babel-plugin-transform-typescript-metadata - D
{
  "plugins": [
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties"],
    "babel-plugin-transform-typescript-metadata"
  ]
}

tsconfig

experimentalDecorators, emitDecoratorMetadata, types and lib compilation options in your tsconfig.json file.

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

usage

import { injectable, inject, useService } from '@rainbow_deer/mini-di'

@injectable()
class A {
  sayHello () {
    console.log('hello! my name is A')
  }
}

@injectable()
class B {
  @inject() a: A
  sayHello () {
    console.log('hello! my name is B')
  }
}

class C {
  @inject() a: A
  @inject() b: B
  sayHello () {
    console.log('hello! my name is C')
  }
}

const c = new C()
const b = useService(B)
c.a.sayHello() // hello! my name is A
c.b.a.sayHello() // hello! my name is A
c.b.sayHello() // hello! my name is B
b.a.sayHello() // hello! my name is A

About

Dependency injection based on typescript+reflect-metadata

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published