angularangular-moduleangular-library

Why do i need *public-api.ts* and also *exports*


I am new to angular,

started writing my first library, containing components, services, directives.

i listed my components, services, directives in the library's exports array, but still:

reading around the docs at angular.io, it looks like public-api.ts and exports serve the same purpose - i am probably missing something basic here.


Solution

  • The exports of a @NgModule defines what is exposed to other modules when that module is imported in the imports of another module. The public-api acts as a barrel file for cleaner path imports.

    Example:

    // Instead of this
    import { ExampleService } from '@lib/services/example.service';
    import { AntoherService } from '@lib/sevivces/another.service';
    
    // You can do this
    import { ExampleService, AnotherService } from '@lib';
    

    https://angular.io/api/core/NgModule#exports https://basarat.gitbook.io/typescript/main-1/barrel