mohammad adibi
mohammad adibi

Reputation: 107

angular, just import component from LIBRARY not whole of library module

I created an angular workspce:

ng new my-workspace --createApplication="false"

then create an app:

ng g application my-app --routing=true --style=scss

then create a library:

ng g library my-lib --prefix=lib

now I want to just import component of library, not whole of library
so import componet library to app module:

import { MyLibComponent } from 'my-lib';
declarations: [AppComponent,MyLibComponent],

then build library and serve app, erverything is ok.
then build app, again everything is ok.
but when build in product mode, i get an error that: 'MyLibComponent is part of 2 module'!

i think here library is another app but in the same workspace.
also when I import library module (instead of library component: import { MyLibModule } from 'my-lib';)it woks!
what is my wrong?

Upvotes: 0

Views: 6638

Answers (1)

Okyam
Okyam

Reputation: 730

If I understand you correctly you want to import just the component to the application called "my-app", That is not possible because Angular works with module what I mean is that modules are the containers of components so you will always need a module to consume your components, what you would do is to create feature modules.

create a module call MyLibModule and then create a component inside that module, so you will just import this module from your library to your app.

Another good reference is this link https://angular.io/guide/module-types, because when you building a library you will have different types of modules (widget, services, and others).

Upvotes: 1

Related Questions