Last active
November 6, 2020 03:38
-
-
Save mildronize/8b52a7acbbf0c30c17fa6da917dee49b to your computer and use it in GitHub Desktop.
Example usage of ReflectiveInjector in `injection-js` ( Angular 4 API) read more: https://v4.angular.io/api/core/ReflectiveInjector ( @Injectable should separate file)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'reflect-metadata'; | |
import { ReflectiveInjector, Injectable, Injector } from 'injection-js'; | |
class Service { | |
get() { | |
return "my service"; | |
} | |
} | |
class Service2 { | |
get() { | |
return "my service 2"; | |
} | |
} | |
@Injectable() | |
class Controller { | |
constructor(public service: Service, public service2: Service2) {} | |
} | |
const injector: Injector = ReflectiveInjector.resolveAndCreate([Service, Controller, Service2]); | |
const controller = injector.get(Controller) as Controller; | |
console.log(controller.service.get()); | |
console.log(controller.service2.get()); | |
/** | |
* Output | |
* my service | |
* my service 2 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment