我希望有一个Angular 6库,它只发布单例服务。
下面是一个简单的Angular库,它只包含这个服务:
test54-library/src/lib/test54-library.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Test54LibraryService {
constructor() { }
testCall() {
console.log('this is a testcall');
}
} test54-library/src/public_api.ts
export * from './lib/test54-library.service';我必须在这个库中有一个这样的模块,在提供者条目中引用这个服务吗?为什么在这个用例中需要一个模块?
@NgModule()
export class Test54Module {
static forRoot():ModuleWithProviders {
return {
ngModuleTest54Module,
providers: [
Test54LibraryService
]
}
}
}发布于 2019-03-11 17:20:37
您不需要再次提供该服务。providedIn: 'root'会为您完成这项工作。
有关更多信息,请参阅:singleton-services。
https://stackoverflow.com/questions/54949693
复制相似问题