user2004
user2004

Reputation: 1973

declare component in feature modules

I have an angular project with the following structure: A main module 'admin' that contains 3 more feature modules. I have a component that I have to use in first and second module, and I have tried to import the component in declarations array of admin.module and I get an error saying component cannot be found in first module.

I have also tried to import the component in in first.module and second.module and I get an error saying 'The class 'SectionBannersComponent' is listed in the declarations of the NgModule 'BannersModule', but is not a directive, a component, or a pipe. Either remove it from the NgModule's declarations, or add an appropriate Angular decorator.'

admin.module
   first.module
   second.module
   another.module

How should I import the component in order to use it in both feature modules?

Upvotes: 1

Views: 184

Answers (1)

Benzara Tahar
Benzara Tahar

Reputation: 2207

if you have a component in the admin module that you want to use in other modules, then you have to import the admin module into those modules (first.module and second.module in your case). However, you don't want to import a parent module into a child module. So the most efficient way is to:

  • create a shared module
  • declare your shared components into the shared module and export them as well (declarations and exports array)
  • import the shared.module into all the modules that use those shared components The shared module can contain other declarations (pipes, directives, common modules exports like 3rd party UI library that is used across the application...etc.)

For your second error, that's because a component cannot be declared into more than one module. The solution above should fix your problem.

Upvotes: 2

Related Questions