angular - 如何在其构造函数中使用 vars 创建单例?
问题描述
我的角度项目中有这个单身人士:
class CustomersServicesHandler
{
private static _instance: CustomersServicesHandler;
bridalEventId: string = null;
private constructor(private _customerService: CustomersService,
private _globalFunctionsService: GlobalFunctionsService)
{
//...
}
public static get Instance()
{
return this._instance || (this._instance = new this());//this is my problem
}
}
如您所见,我在构造函数中使用了一些服务。问题从我创建时开始new this()
- 我需要传递也获取变量的变量(服务)。
解决方案
尝试CustomersServicesHandler
在要使用它的类中注入:
class ClassYouWantToUseItIn {
constructor(private customersServicesHandler: CustomersServicesHandler) {
}
}
CustomersService
Angular 会为你处理和的创建/传递GlobalFunctionsService
。
推荐阅读
- java - 在java中从文件中读取和解析。我怎样才能让它读取和打印狮子等的详细信息
- java - 如何在数学表中打印随机数?
- codenameone - Codenameone - 单击按钮时 Android 上的空指针异常
- irc - UnrealIRCd or InspIRCd
- c++ - Is aliasing an array of T to an array of std::complex
legal? - git - cannot git push due to unrelated upstream file changes
- firebase - _firebaseAuth.updateProfile missing after firebase_auth plugin upgrade to 0.6.2+1
- c# - Azure functions: Move to poison blob without retry if a particular exception is thrown
- android - 如何在辅助/外部屏幕上运行 qt android 应用程序
- php - Laravel - 带有值标签的测试编辑表单