Git Product home page Git Product logo

Comments (6)

mgechev avatar mgechev commented on May 20, 2024

Would you share the declarations of the services as well?

from injection-js.

nxddsnc avatar nxddsnc commented on May 20, 2024

Sorry. Seems that I did not describe my issue correctly. I injected the 'DataService' to several Classes.

from injection-js.

mgechev avatar mgechev commented on May 20, 2024

Would you provide the entire example so I can reproduce the issue?

from injection-js.

nxddsnc avatar nxddsnc commented on May 20, 2024

The entire project is too large. So I create a test example here: https://github.com/nxddsnc/injectorTest.
After building this with webpack, I use it like:
let b = new ServiceB(); let c = new ServiceC();
in the constructors of the class ServiceB and ServiceC, I use this to get the ServiceA:
const injector = ReflectiveInjector.resolveAndCreate([ServiceA]); let serviceA = injector.get(ServiceA);
And there's two logs says "service A constructed";

from injection-js.

mgechev avatar mgechev commented on May 20, 2024

@nxddsnc you should not create an instance of the ReflectiveInjector inside of the classes' constructors. You should turn this code:

import { Injectable, ReflectiveInjector } from 'injection-js';

@Injectable()
export class ServiceA {
  constructor() {
    console.log('service A constructed');
  }
}

@Injectable()
export class ServiceB {
  constructor() {
    const injector = ReflectiveInjector.resolveAndCreate([ServiceA]);
    let serviceA = injector.get(ServiceA);
  }
}

Into:

import 'reflect-metadata';
import { Injectable, ReflectiveInjector } from 'injection-js';

export class ServiceA {
  constructor() {
    console.log('service A constructed');
  }
}

@Injectable()
export class ServiceB {
  constructor(private serviceA: ServiceA) {}
}

const injector = ReflectiveInjector.resolveAndCreate([ServiceA, ServiceB]);
injector.get(ServiceB);

Notice the following:

  • You need reflect-metadata.
  • You need to set emitDecoratorMetadata flag in tsconfig.json to `true.
  • ServiceB is delegating the instantiation of ServiceA to the ReflectiveInjector. This way ServiceB is not coupled with a specific abstraction (i.e. ReflectiveInjector can pass a subclass of ServiceA), neither with the process of instantiation of ServiceA.

from injection-js.

nxddsnc avatar nxddsnc commented on May 20, 2024

Understood. Thank you very much!

from injection-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.