1.0.3 • Published 5 years ago
@all-in-js/injector v1.0.3
a Javascript & Nodejs dependencies injector
- Container class
import { ContainerClass } from '@all-in-js/injector';
const initDeps = {
key: function key() {}
};
const container = new ContainerClass(initDeps);
container.add(class TestService{});
container.resolve(['TestService', 'key'], function(testService, key) {});
// or
const [testService] = container.resolve('TestService');
// this `container.resolve` support various types, eg:
// `container.resolve(string[array<string>[function]])`
// if argument just is a function, it's arguments would be parsed to be an array to be resolved.- Decorator
import { Injectable } from '@all-in-js/injector';
@Injectable
class TestService {}- inject to constructor
import { Inject } from '@all-in-js/injector';
@Inject('TestService')
class TestControler {
constructor(testService) {
this.testService = testService;
}
}- inject to class's function prop
import { Inject } from '@all-in-js/injector';
class TestControler {
@Inject('TestService')
test(testService) {
// ...
}
}- inject to class's value prop
import { Inject } from '@all-in-js/injector';
class TestControler {
@Inject('TestService') testService;
toString() {
// this.testService
}
}- when inject to class's function prop, merge injected value and your arguments.
import { Inject } from '@all-in-js/injector';
class TestControler {
@Inject('TestService')
test(testService, yourArguments) {
// ...
}
}
new TestControler().test('yourArguments');