Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • When using the library form another library or application, and to compile successfully, i needed to list my components and services in the public-api.ts. why ? isn't the ngModule's exports array enough?

  • now deleted the components,services, directives from the exports array and everything still works. why ?

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.

like image 267
Menahem Avatar asked Nov 20 '19 15:11

Menahem


1 Answers

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 { ExmpaleService, AnotherService } from '@lib';

https://angular.io/api/core/NgModule#exports https://basarat.gitbooks.io/typescript/docs/tips/barrel.html

like image 100
Jason White Avatar answered Sep 22 '22 13:09

Jason White