angular - NullInjectorError : No provider for DecimalPipe

标签 angular typescript angular-module

我在我的应用程序中实现了延迟加载。我的一项服务需要包含 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/

    相关文章:

    Angular:从惰性功能模块添加一个多提供者

    angular - Firebase 云函数以状态码 304 结束

    json - 期望字符串时,JSON.parse 位置 0 处的 JSON 中的 Angular Unexpected token c

    javascript - TypeScript 可以与 PhoneGap(或类似的)一起使用吗?

    javascript - 编译成 CommonJS 不会产生 NodeJS 可用的文件

    javascript - 尽管关联的 getter 可以工作,但对 Typescript setter 的调用不会返回函数

    angular - 在两个不同的模块之间共享一个组件

    angular - 如何在 IIS 上使用 Angular 12 部署 ASP.NET Core 6 应用程序

    javascript - Angular 和 Observable : how to avoid multiple requests to API within a given time

    Angular 6 providedIn - 如何自定义 @Injectable() 提供程序以进行依赖注入(inject)?