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

1.1.6 • Public • Published

DependencyInject

Live Demo

import { Container, inject } from 'dependency-inject'
 
class Store {
    num = 1
}
 
class Action {
    @inject(Store)
    private store: Store
 
    setNum(num: number) {
        this.store.num = num
    }
}
 
// init store
const container = new Container()
container.set(Store, new Store())
container.set(Action, new Action())
 
// get data with injected
const store = container.get(Store)
const action = container.get(Action)
 
action.setNum(2)
console.log(store.num) // 2

Simple useage by injectFactory

import { injectFactory } from 'dependency-inject'
class Store {
    num = 1
}
 
class Action {
    @inject(Store)
    private store: Store
 
    setNum(num: number) {
        this.store.num = num
    }
}
 
const stores = injectFactory({Store, Action})
// stores.Store.num === 1
// stores.Action.setNum

nested object

import { injectFactory } from 'dependency-inject'
class Store {
    num = 1
}
 
class Action {
    @inject(Store)
    private store: Store
 
    setNum(num: number) {
        this.store.num = num
    }
}
 
const stores = injectFactory({
    groupA: Store,
    groupB: {
        groupC: Action
    }
})

Readme

Keywords

none

Package Sidebar

Install

npm i dependency-inject

Weekly Downloads

13

Version

1.1.6

License

ISC

Unpacked Size

65 kB

Total Files

43

Last publish

Collaborators

  • ascoders