![](/img/trans.png)
[英]Angular 8: Get instance of service to another service without using constructor
[英]Injecting service singleton instance to another service without constructor
我有:
我想將UtilService單例實例注入BaseService,但不使用構造函數(Constructor(baseService:BaseService){}),因為如果沒有,則AService和BService在調用UtilService的構造函數時都需要將UtilService單例實例作為參數傳遞(super(arguments) ),這顯然是多余的步驟。 所以我想知道是否還有其他方法可以在AService / BService構造函數的超級調用中傳遞UtilService單例實例。 提前致謝。
我不知道會不會是辛格爾頓,但是您可以使用
constructor(private inj: Injector) { }
....where you need..
const customService= this.inj.get(CustomService)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.