我在我的应用程序中实现了延迟加载。我的一项服务需要包含 DecimalPipe。
服务 --> 共享模块 --> 应用程序模块
这是我的结构。我已经在 app.module.ts 中包含了“CommonModule”,我的服务也需要十进制管道。
Type DecimalPipe is part of the declarations of 2 modules: CommonModule and SharedModule! Please consider moving DecimalPipe to a higher module that imports CommonModule and SharedModule. You can also create a new NgModule that exports and includes DecimalPipe then import that NgModule in CommonModule and SharedModule.
因此,既然它已经是 Commons Module 的一部分,为什么不从 Commons Module 获取 Decimal 管道。 ?
如果未声明,则会显示以下错误
NullInjectorError: No provider for DecimalPipe!
请让我知道如何处理此错误。
提前致谢!
最佳答案
您需要在提供服务的模块中提供 DecimalPipe
例如,如果您的服务是“providedIn: 'root'”
那么你应该像这样在你的 AppModule 中提供 DecimalPipe:
import {DecimalPipe} from '@angular/common';
@NgModule({
declarations: [
AppComponent,
],
imports: [ ],
providers: [DecimalPipe],
exports: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
关于angular - NullInjectorError : No provider for DecimalPipe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58277592/