【问题标题】:Unexpected pipe imported by the module 'AppModule'. Please add a @NgModule annotation模块“AppModule”导入的意外管道。请添加@NgModule 注释
【发布时间】:2020-02-28 06:21:24
【问题描述】:

我创建了用于从数组中删除重复项的唯一管道,并将其导入 app.module.ts

这是我的代码。

app.module.ts

import { UniquePipe } from './_pipe/uniquePipe';

@NgModule({
  imports: [BrowserModule, CommonModule, FormsModule, ReactiveFormsModule, HttpClientModule, UniquePipe],
  // tslint:disable-next-line:max-line-length
  declarations: [ AppComponent, UniquePipe],
  exports: [UniquePipe ],
  bootstrap:    [ AppComponent ],
})

uniquePipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import * as _ from 'lodash';


@Pipe({
  name: 'unique',
  pure: false
})
export class UniquePipe implements PipeTransform {
  transform(value: any): any {
    if (value !== undefined && value !== null) {
      return _.uniqBy(value, 'type');
    }
    return value;
  }
}

app.component.html

 <li class="nav-item" *ngFor="let items of list | unique">
      <a class="nav-link" data-toggle="tab" href="#list"> {{items.type}}</a>
    </li>

我该如何解决这个问题

【问题讨论】:

    标签: arrays angular loops pipe


    【解决方案1】:

    只能在declarations 中添加管道,因此只需从imports 中删除管道即可。

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 2019-12-01
      • 2021-09-02
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      相关资源
      最近更新 更多