UNPKG

4.75 kBMarkdownView Raw
1[![Build Status](https://travis-ci.org/mgechev/injection-js.svg?branch=master)](https://travis-ci.org/mgechev/injection-js) ![Downloads](https://img.shields.io/npm/dm/injection-js.svg)
2
3# Dependency Injection
4
5Dependency injection library for JavaScript and TypeScript in **5.2K**. It is an extraction of the Angular's dependency injection which means that it's feature complete, fast, reliable and well tested.
6
7# Why not Angular version 5 and above?
8
9Angular version 5 deprecated the `ReflectiveInjector` API and introduced `StaticInjector`. In short, the dependency injection in the newest versions of Angular will happen entirely compile-time so reflection will not be necessary.
10
11However, if you want to use dependency injection in your Node.js, Vue, React, Vanilla JS, TypeScript, etc. application you won't be able to take advantage of `StaticInjector` the way that Angular will because your application won't be compatible with Angular compiler.
12
13This means that **if you need dependency injection outside of Angular `@angular/core` is not an option. In such case, use `injection-js` for fast, small, reliable, high-quality, well designed and well tested solution.**
14
15# How to use?
16
17```sh
18$ npm i injection-js
19# OR
20$ yarn add injection-js
21```
22
23> **Note:**
24>
25> For ES5 `Class` syntax and TypeScript you need a polyfill for the [Reflect API](http://www.ecma-international.org/ecma-262/6.0/#sec-reflection).
26> You can use:
27>
28> - [reflection](https://www.npmjs.com/package/@abraham/reflection) (only 3kb 🔥)
29> - [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)
30> - [core-js (`core-js/es7/reflect`)](https://www.npmjs.com/package/core-js)
31>
32> Also for TypeScript you will need to enable `experimentalDecorators` and `emitDecoratorMetadata` flags within your `tsconfig.json`
33
34## TypeScript
35
36```ts
37import 'reflect-metadata';
38import { ReflectiveInjector, Injectable, Injector } from 'injection-js';
39
40class Http {}
41
42@Injectable()
43class Service {
44 constructor(private http: Http) {}
45}
46
47@Injectable()
48class Service2 {
49 constructor(private injector: Injector) {}
50
51 getService(): void {
52 console.log(this.injector.get(Service) instanceof Service);
53 }
54
55 createChildInjector(): void {
56 const childInjector = ReflectiveInjector.resolveAndCreate([Service], this.injector);
57 }
58}
59
60const injector = ReflectiveInjector.resolveAndCreate([Service, Http]);
61
62console.log(injector.get(Service) instanceof Service);
63```
64
65## ES6
66
67```js
68const { Inject, ReflectiveInjector } = require('injection-js');
69
70class Http {}
71
72class Service {
73 static get parameters() {
74 return [new Inject(Http)];
75 }
76
77 constructor(http) {
78 this.http = http;
79 }
80}
81
82const injector = ReflectiveInjector.resolveAndCreate([Http, Service]);
83
84console.log(injector.get(Service) instanceof Service);
85```
86
87## ES5
88
89```js
90require('reflect-metadata');
91var di = require('injection-js');
92
93var Http = di.Class({
94 constructor: function() {},
95});
96
97var Service = di.Class({
98 constructor: [
99 Http,
100 function(http) {
101 this.http = http;
102 },
103 ],
104});
105
106var injector = di.ReflectiveInjector.resolveAndCreate([Http, Service]);
107
108console.log(injector.get(Service) instanceof Service);
109```
110
111# API
112
113For full documentation check Angular DI docs:
114
115- [Dependency Injection](https://v4.angular.io/guide/dependency-injection)
116- [Dependency Injection in action](https://v4.angular.io/guide/dependency-injection-in-action)
117- [Dependency Injection without Typescript](https://v2.angular.io/docs/ts/latest/cookbook/ts-to-js.html#!#dependency-injection)
118
119# Ecosystem
120
121This is a list of libraries that are using injection-js. If you have a suggestion on what to add, please don't hesitate to submit a PR.
122
123## Libraries
124
125- [ng-packagr](https://github.com/ng-packagr/ng-packagr) Transpile your libraries to Angular Package Format. Part of the official Angular CLI.
126- [@martin_hotell/axios-http](https://github.com/Hotell/axios-http) Injectable axios HttpClient wrapper for browser and node
127- [@martin_hotell/rea-di](https://github.com/Hotell/rea-di) Dependency injection for React done right. Hierarchical injection on both component and service layer powered by injection-js (Angular DI framework) 🖖
128- [rxstack](https://github.com/rxstack/rxstack) RxStack is a realtime object-oriented framework which helps you build a micro service web applications on top of other frameworks like express and socketio by adding an abstraction layer.
129- [ServeRX-ts](https://github.com/mflorence99/serverx-ts) Experimental [Node.js](https://nodejs.org) HTTP framework using [RxJS](https://rxjs.dev), built with [TypeScript](https://www.typescriptlang.org/) and optimized for serverless deployments. Features declarative routes and dependency injection powered by injection-js.
130
131# License
132
133MIT