After creating a simple Angular library using CLI with the command
ng g library <library-name>
, I encountered an issue while trying to import a component from its module. In my app module, I added components.module to the imports array and attempted to import a components class to another component like this:
import {MyComponent} from ...;
However, my IDE indicated that the Module did not have any exported members with the name of my component. When I checked the module file, it looked like this:
export declare class ComponentsModule {
}
The public_api.d.ts file showed:
export * from './lib/components.service';
export * from './lib/components.component';
export * from './lib/components.module';
Furthermore, in the library-name.d.ts file:
/**
* Generated bundle index. Do not edit.
*/
export * from './public_api';
export { ApiErrorComponent as ɵa } from './lib/modals/api-error/api-error.component';
export { ConfirmationModalComponent as ɵb } from './lib/modals/confirmation-modal/confirmation-modal.component';
export { InfoModalComponent as ɵc } from './lib/modals/info-modal/info-modal.component';
Prior to running ng build, my module source file included:
import {NgModule} from '@angular/core';
import {ComponentsComponent} from './components.component';
import {ApiErrorComponent} from './modals/api-error/api-error.component';
import {ConfirmationModalComponent} from './modals/confirmation-modal/confirmation-modal.component';
import {InfoModalComponent} from './modals/info-modal/info-modal.component';
@NgModule({
imports: [],
declarations: [
ComponentsComponent,
ApiErrorComponent,
ConfirmationModalComponent,
InfoModalComponent,
],
exports: [
ComponentsComponent,
ApiErrorComponent,
ConfirmationModalComponent,
InfoModalComponent,
],
})
export class ComponentsModule {
}
I'm confused about why I can't import these components even after generating the library using Angular CLI. The package contains directories such as ems2015, esm5, fesm5, fesm2015, and bundles beside "lib", but I expected to be able to easily import my module and use its components anywhere I wanted. It seems like they are private or inaccessible, and the documentation on generating and building libraries using CLI is lacking. Any help would be greatly appreciated.