将我的项目更新到最新版本的Angular(6.0.4)后,我想最终将我的代码分成不同的库。
设置如下:
我创建了3个空项目,并开始将所有组件和服务移动到正确的文件夹中。在最终完成所有这些编辑并删除TS错误后,我的构建仍然失败。看来我的CMS应用程序无法找到我库中定义的任何模块,组件或服务。我的库正在构建,似乎包含所有这些元素。
ERROR in ./src/app/app.module.ts
Module not found: Error: Can't resolve '**\Webdictaat.Client\dist\core\core' in '**\Webdictaat.Client\projects\cms\src\app'
我的库包含一个public_api.ts,看起来它正常工作但生成的core.ts文件不包含所有导出的成员。例如, wd-core.module 位于public_api.d.ts的第二行
真正的问题:我的图书馆出了什么问题,并非所有成员都被导出?
//dist/core/public_api.d.ts
export * from './lib/avatar/avatar.component';
export * from './lib/wd-core.module';
export * from './lib/modal-component/modal-component.component';
export * from './lib/game-elements/game-elements.module';
export * from './lib/core/html-outlet.directive';
export * from './lib/services';
export * from './lib/models';
//dist/core/core.d.ts
//No wd-core.module :(
/**
* Generated bundle index. Do not edit.
*/
export * from './public_api';
export { BaseModalService as ɵg } from './lib/core/basemodal.service';
export { GroupByPipe as ɵv } from './lib/core/group-by.pipe';
export { ArraySortPipe as ɵw } from './lib/core/order-by.pipe';
export { ProcessTokenComponent as ɵx } from './lib/core/process-token.component';
export { DirtyGuard as ɵk } from './lib/core/security/dirty.guard';
export { wdApi as ɵb } from './lib/core/wd.service';
export { WdFilterPipe as ɵu } from './lib/core/wdfilter.pipe';
export { DialogComponent as ɵs } from './lib/dialog/dialog.component';
export { ModalComponentComponent as ɵy } from './lib/modal-component/modal-component.component';
export { NavMenuService as ɵm } from './lib/nav-menu/nav-menu.service';
export { AccountService as ɵa } from './lib/services/account.service';
export { AchievementService as ɵi } from './lib/services/achievement.service';
export { AssignmentService as ɵn } from './lib/services/assignment.service';
export { ConfigService as ɵd } from './lib/services/config.service';
export { DialogService as ɵe } from './lib/services/dialog.service';
export { DictaatService as ɵc } from './lib/services/dictaat.service';
export { GoogleAnalyticsEventsService as ɵq } from './lib/services/google-analytics.service';
export { ImageService as ɵh } from './lib/services/images.service';
export { ParticipantService as ɵo } from './lib/services/participantService';
export { PollService as ɵr } from './lib/services/poll.service';
export { QuizService as ɵf } from './lib/services/quiz.service';
export { RatingService as ɵj } from './lib/services/rating.service';
export { StylingService as ɵp } from './lib/services/styling.service';
export { VideoService as ɵl } from './lib/services/video.service';
export { SpinnerComponent as ɵt } from './lib/spinner/spinner.component';
完整的Github项目:
https://github.com/Webdictaat/Webdictaat.Client/tree/update6
图书馆项目
https://github.com/Webdictaat/Webdictaat.Client/tree/update6/projects/core
CMS app项目
https://github.com/Webdictaat/Webdictaat.Client/tree/update6/projects/cms
答案 0 :(得分:3)
您需要做的就是在public_api.ts
文件中将所有需要的内容放在库外,就像第一个答案所示。这是此文件的目的,需要完成,以便您可以正确使用库。 index.ts文件的问题也是真实的,所以如果您在更改之后仍然遇到问题,只需在public_api.ts
中导出所有内容。
然后你从“核心”而不是“核心/核心”进行导入,或者甚至像你现在在代码中那样进行更长的路径导入。例如,在dictaat.module.ts
中,它应如下所示:
import { WdCoreModule, DictaatService } from 'core';
如果它不起作用,则意味着public_api.ts
文件中缺少导出。
答案 1 :(得分:1)
我发现(通过跟踪和错误)Angular 6用来封装你的库的ng-packagr没有正确处理通过"桶"名为" index.ts"的文件由父目录名称引用。
因此,在您的public_api.ts文件中,例如,
export * from './lib/services';
您需要将其更改为
export * from './lib/services/index';
这很烦人,我希望当Angular CLI合并它时,它会被修复,或者至少有很好的记录,但唉,似乎没有。
我实际上并不清楚这是一个ng-packagr问题还是一个Angular问题(可能还有ngc),但https://github.com/dherges/ng-packagr/issues/195有一个问题描述了这个问题on,以及一些Angular问题的链接,以及Angular部分修复它的拉取请求。
换句话说,它很复杂。
与此同时,我发现只需直接引用index.ts文件就可以了。