hook-di
v0.1.0
Published
hook style dependency-injection implement
Downloads
122
Readme
hook-di
Reconsidering dependencies injection
in hook style programming.
Highly inspired by vue composition apis.
usage
here is the interface
s
import { InjectionKey } from 'hook-di'
export interface MyService {
hello: () => string
}
// InjectionKey
export const MyServiceKey: InjectionKey<MyService> = InjectionKey('my-service')
// use MyService hook
export function useMyService(): MyService {
return {
hello() {
return `storea`
},
}
}
manually register dependencies and auto inject.
import { createScope, use } from '../src'
const scope = createScope()
scope.register(MyServiceKey, useMyService)
scope.run(() => {
const service = use(MyServiceKey)
service.hello() // "storebstorea"
})