如何在没有循环依赖的情况下在Angular中创建私有服务
我有一个如何在没有循环依赖的情况下在Angular中创建私有服务,angular,dependency-injection,Angular,Dependency Injection,我有一个ApiService,我不想与组件共享。它仅在其他服务中使用 NGD模块 @NgModule({ providers: [ { provide: 'APIUrl', useValue: window.angularConfig.APIUrl }, // ... declare secret imports like above ], exports: [ FileService, FolderService, ], }) export cl
ApiService
,我不想与组件共享。它仅在其他服务中使用
NGD模块
@NgModule({
providers: [
{ provide: 'APIUrl', useValue: window.angularConfig.APIUrl },
// ... declare secret imports like above
],
exports: [
FileService,
FolderService,
],
})
export class ServicesModule { }
私人服务
@Injectable({
providedIn: ServicesModule,
})
class ApiService {
@Inject('secret') secret!: string;
makeApiCall(url: string) { /* make HTTP call and send secret in header */}
}
公共服务
@Injectable({
providedIn: ServicesModule,
})
class PublicService {
constructor(private api: ApiService ) {}
getNames(url: string) {
return this.api.makeApiCall('url/endpoint')
}
}
此解决方案会给我一个如下错误:
检测到循环依赖项中的警告:
..\services\api.service.ts->..\services.module.ts->..\public.service.ts->..\api.service.ts`你能把同样的复制到吗?@NarendraSinghRathore我已经做了,但是StackBlitz显示了不同的错误。。。您可以检查此链接。我分叉了代码并做了一些更改,试图删除错误,如果有助于解决问题,则标记为有用。@NarendraSinghRathore嗯,我想对其他人“隐藏”
ParentService
,因为它是私有的。它应该只在/services/
@NarendraSinghRathore中使用。看起来您编辑了代码,现在看起来好多了,但我不想向root用户提供apichecsum
(它由父服务使用)我不想使用注入器
…你能在上面复制相同的吗?@NarendraSinghRathore我做了,但是StackBlitz显示了不同的错误。。。您可以检查此链接。我分叉了代码并做了一些更改,试图删除错误,如果有助于解决问题,则标记为有用。@NarendraSinghRathore嗯,我想对其他人“隐藏”ParentService
,因为它是私有的。它应该只在/services/
@NarendraSinghRathore中使用。看起来您编辑了代码,现在看起来好多了,但是我不想向root用户提供apichecsum
(它由父服务使用),我不想使用Injector
。。。