di.js icon indicating copy to clipboard operation
di.js copied to clipboard

example how to use it with typescript

Open pleerock opened this issue 9 years ago • 3 comments

any example how to use it in a typescript 1.5 and integrate with its decorators new module system?

pleerock avatar Apr 09 '15 08:04 pleerock

Try this, it combines di.js library and new typescript annotations.

https://github.com/VaclavObornik/di-ts

VaclavObornik avatar Apr 18 '15 11:04 VaclavObornik

Hello!

Thank you for this great module and your hard work!

However, I'm very interested to use it in my next project with TypeScript 2.2 and Node.js. Is it possible? If so, what are your recommendations, how do I integrate it with TypeScript, is there a typings?

Sadly, @VaclavObornik 's module appears to be severely outdated and broken (and all the other modules I've also found).

@vojtajina could you elaborate on this please? Thank you!

slavafomin avatar Apr 03 '17 00:04 slavafomin

If someone is interested, I've found a way to use DI from Angular 2 directly. Please read this comprehensive material: https://angular.io/docs/ts/latest/guide/dependency-injection.html#!#di-metadata.

Example:


import 'reflect-metadata';

import {ReflectiveInjector, Injectable} from '@angular/core';

@Injectable()
class Engine {
  constructor () {
    console.log('Engine is created');
  }
}

@Injectable()
class Car {
  constructor (public engine: Engine) {
    console.log('Car is created', engine);
  }
}

let injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);

const car = injector.get(Car);

You will also need the following in your tsconfig.json:

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

And don't forget to install rxjs.

slavafomin avatar Apr 03 '17 01:04 slavafomin