@eryue/injectordeprecated

a Javascript & Nodejs dependencies injector

Usage no npm install needed!

<script type="module">
  import eryueInjector from 'https://cdn.skypack.dev/@eryue/injector';
</script>

README

npm Build Status Coverage Status

a Javascript & Nodejs dependencies injector

Usage

  • Container class
import { ContainerClass } from '@eryue/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 '@eryue/injector';

@Injectable
class TestService {}
  • inject to constructor
import { Inject } from '@eryue/injector';

@Inject('TestService')
class TestControler {
  constructor(testService) {
    this.testService = testService;
  }
}
  • inject to class's function prop
import { Inject } from '@eryue/injector';

class TestControler {
  @Inject('TestService')
  test(testService) {
    // ...
  }
}
  • inject to class's value prop
import { Inject } from '@eryue/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 '@eryue/injector';

class TestControler {
  @Inject('TestService')
  test(testService, yourArguments) {
    // ...
  }
}

new TestControler().test('yourArguments');