0

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?

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

6 Comments

thank you may be you are right, but in some tutorial import like this, let me check more.
Check it, Base on the documentation you will need a module, what others library that I use does is to create modules of their components and you can import them separately
Hey, can you please check my answer, if it helps you.
you were correct okyam, but what is 'export * from './lib/mylib.component';' for in public api ts? it confuses me!
The purpose of this file just exposes the functionality that you want your library export, and the consumer will do import * from ./public_api', but you should export the modules that your components need to work anyway, To achieve what you want that is to import a specific component then you should export the module in the public API ts and then you will have all components that this module export if you have one then you will just export one but you will need a module..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.