I'm planning on making a standalone angular service for Angular 8+, and I've been reading up on how to make it tree tree-shakable.
(我正计划为Angular 8+提供独立的角度服务,并且我一直在阅读如何使其可树状摇动。)
IIUC all we need to do is this :
(IIUC我们需要做的就是 :)
@Injectable({
providedIn: 'root',
useFactory: () => new Service('dependency'),
})
export class Service {
constructor(private dep: string) {
}
}
As this tells Angular how to construct the service.
(这样就告诉了Angular如何构建服务。)
Is this all we need to do?
(这就是我们需要做的吗?)
I've seen other NPM modules like this one use ModuleWithProviders
like this:(我看过其他类似这样的 NPM模块, 使用了这样的 ModuleWithProviders
:)
export class Module {
static forRoot(): ModuleWithProviders {
return {
ngModule: Module,
providers: [
StripeScriptTag
],
}
}
I assume that this is not necessary because the providers
array still does not describe how to instantiate the service.
(我认为这不是必需的,因为providers
数组仍然没有描述如何实例化服务。)
ask by Ole translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…