Angular 7-自定义库-无法导出简单类

时间:2019-05-16 12:40:19

标签: angular

我创建了一个自定义库,该库已发布供我在应用程序中使用

import { AccordionComponent } from 'my-angular-lib';
import { AccordionItemComponent } from 'my-angular-lib';

这两个组件确实包含在内并且可以正确显示

但是,当我添加一个不是指令/组件的简单类时。

export class Tools {
...
}

只是在我的库模块中找不到在我编译应用程序时

ERROR in src/app/app.component.ts(10,10): error TS2305: Module '"...../node_modules/my-angular-lib/my-angular-lib"' has no exported member 'Tools'.

尽管我在 public-api.ts 中声明了它,但仍然无法使用

export * from './lib/helpers/tools.class';

我也尝试将其添加到主库模块中,但它抱怨它没有 @ Directive / @ Component 标签

这就是我在应用程序中使用它的方式

import { Tools } from 'my-angular-lib';

所有其他组件都可以使用,并且可以在我的库的名称空间中找到

我已经读了很多线程,但是没有一个给我一个替代答案来解决这个问题

对此表示感谢

[编辑:]

如果我尝试将其添加到导入/导出中,则会得到

 Please add a @Pipe/@Directive/@Component annotation.

为什么我不能只导出它?

my-angular-lib.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';  
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AccordionComponent } from './widgets/accordion/accordion.component';
import { AccordionItemComponent } from './widgets/accordion/accordionItem.component';

import { PaginationComponent } from './widgets/pagination/pagination.component';
import { PaginationService } from './widgets/pagination/pagination.service';

import { PasswordComplexityComponent } from './widgets/passwordComplexity/passwordComplexity.component';

import { UpDownComponent } from './widgets/updown/updown.component';

import { Tools } from './helpers/tools.class';

@NgModule({
  declarations: [
      AccordionComponent,
      AccordionItemComponent,
      PaginationComponent,
      PasswordComplexityComponent,
      UpDownComponent,
      Tools
  ],
  imports: [
      CommonModule,
      FormsModule
      ],
  exports: [
      AccordionComponent,
      AccordionItemComponent,
      PaginationComponent,
      PasswordComplexityComponent,
      UpDownComponent,
      Tools
  ],
  providers:[PaginationService]
})
export class MyAngularLibModule { }

public-api.ts

export * from './lib/my-angular-lib.module';

export * from './lib/widgets/accordion/accordion.component';
export * from './lib/widgets/accordion/accordionItem.component';

export * from './lib/widgets/pagination/pagination.component';
export * from './lib/widgets/pagination/pagination.service';

export * from './lib/widgets/passwordComplexity/passwordComplexity.component';

export * from './lib/widgets/updown/updown.component';

export * from './lib/helpers/tools.class';

2 个答案:

答案 0 :(得分:0)

好吧,我终于做到了

不太确定出了什么问题,我用新的package.json版本重新编译了库,然后重新发布了

突然发现我的班级

奇怪,因为我昨天做了无数次

答案 1 :(得分:0)

在我的案例中,删除声明关键字解决了构建问题,

export declare class myClass{
    ...
}

转到

export class myClass{
    ...
}