Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of public-api.ts in Angular Material/ Angular Flex Layout?

I learned from angular-material and angular-flex-layout that when they export a component, they first export the relevant sub-components in public-api.ts and then just export everything from the public-api.ts in the index.ts (e.g. export * from './public-api'). Here is the example of its badge component:

// public-api.ts export * from './badge-module'; export * from './badge';

// index.ts export * from './public-api';

My question is why don't just put all the export things to index.ts but border to create one more public-api.ts?

like image 866
goldenbearkin Avatar asked May 03 '18 11:05

goldenbearkin


People also ask

What is Flex layout in Angular material?

Angular Flex-Layout is a stand-alone library developed by the Angular team for designing sophisticated layouts. When creating an HTML page in Angular, using Angular Flex-Layout allows us to easily create FlexBox-based page layouts with a set of directives available for use in your templates.

How do I add layout to Angular Flex?

From your project folder, run the following command to install Flex Layout: npm install @angular/flex-layout @10.0. 0-beta. 32.

What is fxLayout column?

fxLayout defines the flow of children elements along the main-axis or cross-axis, inside the flex container. Depending upon our layout we can pass four different values to the fxLayout attribute row,column, row-reverse and column-reverse. In addition to the fxLayout accepts other parameters like wrap and inline.

What is Angular layout?

Angular Material's Layout features provide sugar to enable developers to more easily create modern, responsive layouts on top of CSS3 flexbox. The layout API consists of a set of Angular directives that can be applied to any of your application's HTML content.


2 Answers

To keep the code cleaner.

Because 'index' doesn't mean much. But 'public-api' makes it clear what is there, that is, public entities

public_api.ts is intended to enumerate and expose specific functionality for external use. index.ts is intended as a default exporting mechanism to deprecate full-path imports.

Using public_api.ts in libraries instead of piling everything in index.ts

like image 168
Alex Parloti Avatar answered Sep 18 '22 16:09

Alex Parloti


At a glance I think there are several reasons:

  1. Disambiguation: Given the lack of an "internal" modifier and the complexity added by internal modules, more than one index.ts barrel file is required to offer distinct internal/external API surfaces.
  2. Angular CLI user-facing tooling: To offer low-effort tooling defaults, a public API file standard was required. The NG compiler does more than just builds all the TypeScript to JavaScript; notably it also tree-shakes depending on the entrypoint chosen. For other reasons here, index.ts was a poor choice for the default. See Creating Libraries.
  3. Angular internal code quality: The public-api is the contract with Angular consumers and must be tightly controlled, with changes accompanied by documentation changes, new tests, compatibility evaluation, and announcements. To do so, project settings automatically reject code changes that result in changes to public-api. See Golden Files.
  4. Awareness/Clarity/Intent: I think this is generally what the other comments have hinted at. People have historically been using index.ts for everything, both internal and external. Sure, a project could use index.ts only for public API signatures, but it wouldn't be obvious.
like image 20
shannon Avatar answered Sep 21 '22 16:09

shannon