NullInjectorError:DecimalPipe 没有提供程序

Shr*_*ani 13 typescript angular-module angular

我在我的应用程序中实现了延迟加载。我的一项服务需要包含 DecimalPipe。

服务 --> 共享模块 --> 应用模块

这是我的结构。我已经在 app.module.ts 中包含了“CommonModule”,我的服务也需要 Decimal Pipe。

  1. 在我的共享模块中包含“十进制管道”会出现以下错误:

类型 DecimalPipe 是 2 个模块声明的一部分:CommonModule 和 SharedModule!请考虑将 DecimalPipe 移至导入 CommonModule 和 SharedModule 的更高模块。您还可以创建一个新的 NgModule 来导出并包含 DecimalPipe,然后在 CommonModule 和 SharedModule 中导入该 NgModule。

所以既然它已经是 Commons Module 的一部分,为什么不从 Commons Module 中取出 Decimal 管道呢?? 如果未声明,则会显示以下错误

NullInjectorError:DecimalPipe 没有提供程序!

请让我知道如何处理此错误。提前致谢!

uri*_*eld 16

您需要在提供服务的模块中提供 DecimalPipe

例如,如果您的服务是“providedIn: 'root'”,那么您应该像这样在 AppModule 中提供 DecimalPipe:

import {DecimalPipe} from '@angular/common';
@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [ ],
  providers: [DecimalPipe],
  exports: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)