在Angular 8中从外部模块公开管道

我正在抽象一些我们在项目中使用的实用程序库。 我创建了一个新的Angular库,其中包含一些模型,服务和管道。

我需要能够在模板和组件内部使用管道。

这是我的图书馆结构:

共享模块

@NgModule({
  providers: [
    BytesPipe
  ],declarations: [
    BytesPipe
  ],exports: [
    BytesPipe
  ]
})

ByesPipe

   @Pipe({
          name: 'bytes'
        })
        export class BytesPipe implements PipeTransform {         
          transform(input: any): any {
...

         }
        }

构建库,然后在应用程序中进行导入: App.module

@NgModule({
  declarations: [...],imports: [Sharedmodule],exports: [Sharedmodule]
})
export class AppModule { }

如果我在模板中使用BytesPipe,则一切正常。 当我尝试在组件内部使用Pipe时,收到此编译错误:

Module not found: Error: Can't resolve 'shared/lib/pipe/bytes.pipe' 

使工作正常进行的唯一方法是执行此导入,而不是“ shared / lib / pipe / bytes.pipe”:

import {ɵe as BytesPipe} from 'shared'

“ shared.d.ts”的内容为:

export * from './public-api';
export { BytesPipe as ɵe } from './lib/pipe/bytes.pipe';

我知道这种导入方式是错误的,因为我指的是经过不同构建可能会更改的已编译名称。

我做错了什么?

wmq890614 回答:在Angular 8中从外部模块公开管道

答案很简单:我需要在public_api上显式导出管道:

export { BytesPipe } from './lib/pipe/bytes.pipe'
本文链接:https://www.f2er.com/3117162.html

大家都在问