Inject dependencies into injectable classes


002.2.03 years ago4 years agoMinified + gzip package size for @jable/inject in KB



Inject dependencies into injectable classes


export class Bar {
    constructor(public barProp: string = "Bar") {}

export class Foo {
    constructor(public myString: string, public bar: Bar) {}

var myFoo = Injector.inject(Foo, "myString"); // Injector syntax
console.log(myFoo.myString == "myString"); // True
console.log(myFoo.bar.barProp == "Bar"); // True
myFoo.bar.barProp = "FooBar"

var myFoo2: Foo = new (<any>Foo)("mySecondString"); // TS syntax
console.log(myFoo2.myString == "mySecondString"); // True
console.log(myFoo2.bar.barProp == "FooBar"); // True


 * Injectable options
 * @export
 * @interface IInjectableOptions
 * @property {string} namespace Namespace of the Injectable
 * @property {{[type: string]: boolean}} exclude Excluded namespaced types, true to exclude
 * @property {boolean} inject Whether or not the class should have injectables injected into it
export interface IInjectableOptions {
    namespace?: string;
    exclude?: {[type: string]: boolean};
    inject?: boolean;

const defaultOptions: IInjectableOptions = {
    namespace: '',
    exclude: {
        String: true,
        Number: true,
        Object: true,
        Array: true,
        Boolean: true
    inject: true

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.