【问题标题】:Cannot declare 'TimeAgoPipe' in an NgModule as it's not a part of the current compilation无法在 NgModule 中声明“TimeAgoPipe”,因为它不是当前编译的一部分
【发布时间】:2021-01-20 12:14:53
【问题描述】:

我在执行ng build --prod 时遇到Angular 10 错误。它适用于ng build

ERROR in src/app/app.module.ts:44:5 - error NG6001: Cannot declare 'TimeAgoPipe' in an NgModule as it's not a part of the current compilation.

44     TimeAgoPipe,
       ~~~~~~~~~~~

  node_modules/time-ago-pipe/time-ago.pipe.d.ts:3:22
    3 export declare class TimeAgoPipe implements PipeTransform, OnDestroy {
                           ~~~~~~~~~~~
    'TimeAgoPipe' is declared here.

这是node_modules/time-ago-pipe/time-ago.pipe.d.ts里面的文件

import { PipeTransform, NgZone, ChangeDetectorRef, OnDestroy } from "@angular/core";
import * as ɵngcc0 from '@angular/core';
export declare class TimeAgoPipe implements PipeTransform, OnDestroy {
    private changeDetectorRef;
    private ngZone;
    private timer;
    constructor(changeDetectorRef: ChangeDetectorRef, ngZone: NgZone);
    transform(value: string): string;
    ngOnDestroy(): void;
    private removeTimer();
    private getSecondsUntilUpdate(seconds);
    static ɵfac: ɵngcc0.ɵɵFactoryDef<TimeAgoPipe, never>;
    static ɵpipe: ɵngcc0.ɵɵPipeDefWithMeta<TimeAgoPipe, "timeAgo">;
}

//# sourceMappingURL=time-ago.pipe.d.ts.map

这就是我在app.module.ts 文件中使用它的方式

import { NgModule } from '@angular/core';
import {TimeAgoPipe} from 'time-ago-pipe';

@NgModule({
  declarations: [
    TimeAgoPipe
  ],
  imports: [
    FormsModule,
  ],
  entryComponents: [LastseenDevicesComponent],
  bootstrap: [ AppComponent ],
})
export class AppModule { }

我该如何解决这个问题?

【问题讨论】:

    标签: angular ng-modules


    【解决方案1】:

    这个包没有维护3年,我建议使用https://www.npmjs.com/package/ngx-pipes#timeago,你可以在这里找到更多关于这个问题的细节https://github.com/AndrewPoyntz/time-ago-pipe/issues/33

    此解决方案适用于某些人

    ...
    import { TimeAgoPipe } from 'time-ago-pipe';
    
    @Pipe({
        name: 'timeAgo',
        pure: false
    })
    export class TimeAgoExtendsPipe extends TimeAgoPipe {}
    
    @NgModule({
        declarations: [
            TimeAgoExtendsPipe,
    ...
    

    【讨论】:

      【解决方案2】:

      TimeAgoPipe 也添加到app.moduleexports 数组中。

      【讨论】:

      • app.module.ts 中似乎没有export 数组。
      • 你可以像imports一样添加exports数组。
      • 试过这个但同样的错误exports: [TimeAgoPipe],
      猜你喜欢
      • 2020-06-21
      • 2020-09-09
      • 2011-09-16
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多