How to use secondary endpoints to create API for Angular library

送分小仙女□ 提交于 2021-01-29 14:10:34

问题


Scenario:

I created a custom UI library using ng-packagr, exported custom components and some model classes.

Problem:

In main project, import statement works fine for exported components but it doesn't work for exported classes and webpack fails to compile giving error:

module not found error can't resolve my-custom-library/models

Library project code:

Model class: src/app/modules/my-custom-module/models/my-model.ts

export class MyModel {
  constructor() {
    // default values for props here
  }

  // ...props here
}

Index file: src/app/modules/my-custom-module/models/index.ts

export * from './my-model';
export * from './my-other-model';

Export file at root: models.ts

export * from './src/app/modules/my-custom-module/models/index';

ng-packagr file at root: public_api.ts

export * from './models';

export * from './src/app/modules/my-custom-module/my-custom-module.module';
export * from './src/app/modules/my-custom-module/components/index';

Main project code:

import {MyCustomModule} from 'my-custom-library'; // works
import {MyModel} from 'my-custom-library'; // works
import {MyModel} from 'my-custom-library/models'; // does not works

Purpose: Simplifying and dividing the UI library API for end developers.

I believe, I'm missing something about mapping webpack module resolution because VS Code recognizes the import, intellisense works and even Ctrl + Click navigates to right file but webpack fails in compilation.

Git Issue


回答1:


I was able to achieve my required API pattern using Seconday End Points.

Credits: Alan Agius at Git Issue



来源:https://stackoverflow.com/questions/51186867/how-to-use-secondary-endpoints-to-create-api-for-angular-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!