4

I have an Angular2 application with submodules:

├───app (module)                                 
│   ├───authentication (module)                 
│   │   ├───components                  
│   │   │   ├───login                   
│   │   │   ├───sign-in                 
│   │   │   ├───sign-up                 
│   │   │   ├───socials-callback        
│   │   │   └───socials-sign-in         
│   │   ├───models                      
│   │   └───services                    
│   ├───components                      
│   │   └───nav-bar                     
│   ├───home (module)                           
│   ├───shared (module)                          
│   │   ├───components                    
│   │   │   └───search-dropdown         
│   │   └───services                    
│   └───user (module)                             
│       ├───models                      
│       └───services                     

Each components are declared in the module_name.module.ts file (of their module). Each modules files are imported in the app.module.ts file.

Here is my app.module.ts file:

import { BrowserModule } from '@angular/platform-browser';
import ...

import { SharedModule }    from './shared/shared.module';

@NgModule({
  imports: [
    AppRoutingModule,
    ...
    SharedModule
  ],
  declarations: [
    AppComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

My shared.module.ts file:

import { NgModule } from '@angular/core';
import ...

import { SearchDropdownComponent } from './components/search-dropdown/search-dropdown.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    SearchDropdownComponent
  ]
})
export class SharedModule { }

When I try to access to SearchDropdownComponent from a template of my app module, I have an error telling my that there is no component corresponding to it.

But when I add the SearchDropdownComponent import directly on my app.module.ts file, it works.

Isn't it possible to access to a component of a sub module? I am doing something wrong?

1 Answer 1

10

To make a component available outside the module, add it the module's exports declaration.

import { NgModule } from '@angular/core';
import ...

import { SearchDropdownComponent } from './components/search-dropdown/search-dropdown.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    SearchDropdownComponent
  ],
  exports: [
    SearchDropdownComponent
  ]
})
export class SharedModule { }

API Reference: NgModule.exports

Also note that a component can belong to only one module.

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

6 Comments

Does that mean that if I have a module A with two submodules B and C, if I export B component in module A, it cannot be used in module C?
You're probably referring to the statement "a component can belong to only one module" - that's true. It means that a component x can "belong" (appear in the declarations) of either module A, B or C. But this has nothing to do with being able to use it - to use a component x in another module, you import the module that exports the component x. Let's say you want to use a component that is declared in module B. You export it from B, and import module B in modules A and C.
Had the same issue, applied this fix and it worked. However, why is it necessary to duplicate declarations in exports? Is this a performance optimisation?
@AdrianMoisa, you may want, for instance, to have some components internal to the module - in that case you would not add them to exports and that's one reason why the there are separate properties.
@RonaldZarīts You mean similar to public/private for class methods?
|

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.