我有以下服务:
SecuredStorageService()
ApiService({this.authService})
AuthService({this.securedStorageService, this.apiService})
RegisterService({this.apiService, this.securedStorageService})
这导致我写:
providers: [
Provider<SecuredStorageService>.value(value: SecuredStorageService()),
ProxyProvider<AuthService, ApiService>(
builder: (_, auth, __) => ApiService(authService: auth),
),
ProxyProvider2<ApiService, SecuredStorageService, RegisterService>(
builder: (_, api, storage, __) => RegisterService(apiService: api, securedStorageService: storage),
),
ProxyProvider2<ApiService, SecuredStorageService, AuthService>(
builder: (_, api, storage, __) => AuthService(apiService: api, securedStorageService: storage),
),
],
到目前为止,我可以说它看起来已经很乱了。但事实并非如此。当我运行该应用程序时,我收到以下错误:
那我怎么办?我在所有ProxyProvider
sa之前添加Provider<AuthService>
。但是,AuthService 被构建了两次!这失去了作为一个单一实例的全部意义(或者不是吗?)。
我的主要目标是像在 Angular 或 Laravel 中一样进行依赖注入。