angular引用管道pipe时踩的坑(Type UmberUnitPipe is part of the declarations of 2 modules:)

我的管道pipe都是统一引入到模块pipeModule中,再在app.module.ts中import这个模块的

umberUnit.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser'

@Pipe({
  name: 'umberUnit'
})
export class UmberUnitPipe implements PipeTransform {

  constructor(
    private ppTest: DomSanitizer
  ) {}

  transform(value: any, args?: any): any {
    if (value < 1000) {
      return value/100 + '百' + args
    } else if (value < 10000) {
      return value/1000 + '千' + args
    }
  }

}

pipeModule.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { PipeModuleComponent } from './pipeModule.component';
import { TranHtmlPipe } from '../tranHtml.pipe';
import { UmberUnitPipe } from '../umberUnit.pipe';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [
    TranHtmlPipe,
    UmberUnitPipe
  ],
  declarations: [
    PipeModuleComponent, 
    TranHtmlPipe,
    UmberUnitPipe
  ]
})
export class PipeModuleModule {
  static forRoot() {
    return {
        ngModule: PipeModuleModule,
        providers: [],
    };
 }
}

app.module.ts

正常情况下,此时就可以直接使用了

但是我发现用的时候一直报错

找了好久的原因,后来发现原来是新建pipe文件时的一个坑

建pipe文件两种方式: 

1、ng g pipe numberUnit(通过密令新建就没坑)

2、vscode自带新建文件的功能 (我是通过第二种方式建的文件)

第二种方式与第一种的区别就是在app-routing.module.ts中也引入了该管道

所以就一直有上面的错误

所以建议新建管道 ,指令时还是通过密令,如果硬是通过vscode新建pipe的话记得把app-routing.module.ts中引入的pipe删掉;至于指令我还不知道会有什么坑,所以我在写的过程中还是会用vscode新建,先为大家趟趟水 ,踩踩坑。

已经给大家试过水了,指令和管道一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值