There are several classes, how injected TermsExchangeService
:
import {TermsExchangeService} from './terms-exchange.service';
@injectable()
export class TermsExchangeComponent {
constructor(
private termsExchangeService: TermsExchangeService
) {}
}
and
import {TermsExchangeService} from './terms-exchange.service';
@injectable()
export class PurchaseMainComponent {
constructor(
private termsExchangeService: TermsExchangeService
) {}
and service how has been used in PurchaseMainComponent
and TermsExchangeComponent
@injectable()
export class TermsExchangeService {
constructor() {
debugger; // call 2 times!!!
}
}
I use autoBindInjectable
:
var simpleContainer = new Container({ autoBindInjectable: true })
When getting components from loop constructor of TermsExchangeService
call several times:
for (let item = 0; item < components.length; item++) {
const container = simpleContainer.get(components[item]);
}
Why object created in every components? And how do normal injections?
By default injectable
is not Singleton.
Solve this problem can adding defaultScope: 'Singleton'
:
var simpleContainer = new Container(
{
autoBindInjectable: true,
defaultScope: 'Singleton'
}
)