Use component from another module
Asked Answered
J

8

312

I have Angular 2.0.0 app generated with angular-cli.

When I create a component and add it to AppModule's declarations array it's all good, it works.

I decided to separate the components, so I created a TaskModule and a component TaskCard. Now I want to use the TaskCard in one of the components of the AppModule (the Board component).

AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { BoardComponent } from './board/board.component';
import { LoginComponent } from './login/login.component';

import { MdButtonModule } from '@angular2-material/button';
import { MdInputModule } from '@angular2-material/input';
import { MdToolbarModule } from '@angular2-material/toolbar';

import { routing, appRoutingProviders} from './app.routing';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';

import { UserService  } from './services/user/user.service';
import { TaskModule } from './task/task.module';


@NgModule({
  declarations: [
    AppComponent,
    BoardComponent,// I want to use TaskCard in this component
    LoginComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MdButtonModule,
    MdInputModule,
    MdToolbarModule,
    routing,
    TaskModule // TaskCard is in this module
  ],
  providers: [UserService],
  bootstrap: [AppComponent]
})
export class AppModule { }

TaskModule:

import { NgModule } from '@angular/core';
import { TaskCardComponent } from './task-card/task-card.component';

import { MdCardModule } from '@angular2-material/card';

@NgModule({
  declarations: [TaskCardComponent],
  imports: [MdCardModule],
  providers: []
})
export class TaskModule{}

The whole project is available on https://github.com/evgdim/angular2 (kanban-board folder)

What am I missing? What do I have to do to use TaskCardComponent in BoardComponent?

Jolly answered 20/9, 2016 at 18:55 Comment(0)
E
548

The main rule here is that:

The selectors which are applicable during compilation of a component template are determined by the module that declares that component, and the transitive closure of the exports of that module's imports.

So, try to export it:

@NgModule({
  declarations: [TaskCardComponent],
  imports: [MdCardModule],
  exports: [TaskCardComponent] // <== export the component you want to use in another module
})
export class TaskModule{}

What should I export?

Export declarable classes that components in other modules should be able to reference in their templates. These are your public classes. If you don't export a class, it stays private, visible only to other component declared in this module.

The minute you create a new module, lazy or not, any new module and you declare anything into it, that new module has a clean state(as Ward Bell said in https://devchat.tv/adv-in-angular/119-aia-avoiding-common-pitfalls-in-angular2)

Angular creates transitive module for each of @NgModules.

This module collects directives that either imported from another module(if transitive module of imported module has exported directives) or declared in current module.

When angular compiles template that belongs to module X it is used those directives that had been collected in X.transitiveModule.directives.

compiledTemplate = new CompiledTemplate(
    false, compMeta.type, compMeta, ngModule, ngModule.transitiveModule.directives);

https://github.com/angular/angular/blob/4.2.x/packages/compiler/src/jit/compiler.ts#L250-L251

enter image description here

This way according to the picture above

  • YComponent can't use ZComponent in its template because directives array of Transitive module Y doesn't contain ZComponent because YModule has not imported ZModule whose transitive module contains ZComponent in exportedDirectives array.

  • Within XComponent template we can use ZComponent because Transitive module X has directives array that contains ZComponent because XModule imports module (YModule) that exports module (ZModule) that exports directive ZComponent

  • Within AppComponent template we can't use XComponent because AppModule imports XModule but XModule doesn't exports XComponent.

See also

Expunge answered 20/9, 2016 at 18:58 Comment(10)
How do I use this "TaskCardComponent" in a route definition in the Importing module?Sedgemoor
What a great answer. Did you create the drawing? If so, I'm speechless. Not everyone put such effort in their answers. Thank youOxycephaly
@Royi Yes, this is my picture :) It's based on source code from github.com/angular/angular/blob/master/packages/compiler/src/…Expunge
@yuruzi, i cannot pass the dom node directly without the reference #47247138 plnkr.co/edit/DnnjFBa3HLzFKNIdE4q5?p=previewFucus
@Expunge ... I don't understand how can YComponent export ZModule, since I am seeing that as a separate file (y.module.ts) and it doesn't have any import so as to export other module (which is z.module.ts) [bear me if its basic question]Invercargill
Hi @Expunge I have this confusion, lets say I have a module A which exports 10 components. If I import this module A into another module B(which needs just one component from module A) 1. Will it import all the classes exported from module A? 2. If Yes how can I avoid it. 3. Does it depend of what I import from module A will be added to module B 4. Will it affect memory or performance to load entire module with 10 components thoe u need 1. I was trying to find the answers to this but could not find a suitable answer.Bolter
one of the best answers i have ever seen in SO......Unrivalled
The best answer ever, he asked for a finger he gave him a hand xD, thanks broAdhere
Does importing a module (that exports the wanted component), affects the bundle size of the destination module, for example if my destination module uses only 1 on 4 components that the source module has, will this make the desitnatino module bundle include the 4 components or just 1?Portraitist
How to make use of other components if my component does not have its own module? In my case this component does not have the module, because it overrides another, built-in 3rd party component in some 3rd party module which I don't have access to.... I've tried to import other components module in app.module, but got site loading loop...Unrefined
V
115

(Angular 2 - Angular 7)

Component can be declared in a single module only. In order to use a component from another module, you need to do two simple tasks:

  1. Export the component in the other module
  2. Import the other module, into the current module

1st Module:

Have a component (lets call it: "ImportantComponent"), we want to re-use in the 2nd Module's page.

@NgModule({
declarations: [
    FirstPage,
    ImportantComponent // <-- Enable using the component html tag in current module
],
imports: [
  IonicPageModule.forChild(NotImportantPage),
  TranslateModule.forChild(),
],
exports: [
    FirstPage,
    ImportantComponent // <--- Enable using the component in other modules
  ]
})
export class FirstPageModule { }

2nd Module:

Reuses the "ImportantComponent", by importing the FirstPageModule

@NgModule({
declarations: [
    SecondPage,
    Example2ndComponent,
    Example3rdComponent
],
imports: [
  IonicPageModule.forChild(SecondPage),
  TranslateModule.forChild(),
  FirstPageModule // <--- this Imports the source module, with its exports
], 
exports: [
    SecondPage,
]
})
export class SecondPageModule { }
Vocoid answered 26/1, 2019 at 19:21 Comment(1)
Although not as verbose as the original, these are the two things you need to do and exporting the component is the one most ofter forgotten. This is a simper answer.Faires
M
45

You have to export it from your NgModule:

@NgModule({
  declarations: [TaskCardComponent],
  exports: [TaskCardComponent],
  imports: [MdCardModule],
  providers: []
})
export class TaskModule{}
Mclyman answered 20/9, 2016 at 18:58 Comment(1)
This Works, till TaskModule is imported in AppModule. It fails when TaskModule is lazyloaded.Alginate
C
4

Note that in order to create a so called "feature module", you need to import CommonModule inside it. So, your module initialization code will look like this:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { TaskCardComponent } from './task-card/task-card.component';
import { MdCardModule } from '@angular2-material/card';

@NgModule({
  imports: [
    CommonModule,
    MdCardModule 
  ],
  declarations: [
    TaskCardComponent
  ],
  exports: [
    TaskCardComponent
  ]
})
export class TaskModule { }

More information available here: https://angular.io/guide/ngmodule#create-the-feature-module

Cheeseparing answered 8/12, 2017 at 14:11 Comment(0)
E
0

Whatever you want to use from another module, just put it in the export array. Like this-

 @NgModule({
  declarations: [TaskCardComponent],
  exports: [TaskCardComponent],
  imports: [MdCardModule]
})
Elrod answered 25/8, 2017 at 17:42 Comment(0)
F
0

One big and great approach is to load the module from a NgModuleFactory, you can load a module inside another module by calling this:

constructor(private loader: NgModuleFactoryLoader, private injector: Injector) {}

loadModule(path: string) {
    this.loader.load(path).then((moduleFactory: NgModuleFactory<any>) => {
        const entryComponent = (<any>moduleFactory.moduleType).entry;
        const moduleRef = moduleFactory.create(this.injector);
        const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
        this.lazyOutlet.createComponent(compFactory);
    });
}

I got this from here (archived).

Fulgurite answered 4/9, 2018 at 17:10 Comment(1)
NgModuleFactoryLoader is deprecated now so what is its best alternate way to do this thing?Whereof
A
0

For anyone who is struggling even after you exported the component in your shared module. Make sure the feature component (the place where you use the shared component using the selector) is declared correctly in the feature module.

Acquaint answered 15/2 at 6:22 Comment(0)
V
-4

SOLVED HOW TO USE A COMPONENT DECLARED IN A MODULE IN OTHER MODULE.

Based on Royi Namir explanation (Thank you so much). There is a missing part to reuse a component declared in a Module in any other module while lazy loading is used.

1st: Export the component in the module which contains it:

@NgModule({
  declarations: [TaskCardComponent],
  imports: [MdCardModule],
  exports: [TaskCardComponent] <== this line
})
export class TaskModule{}

2nd: In the module where you want to use TaskCardComponent:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MdCardModule } from '@angular2-material/card';

@NgModule({
  imports: [
   CommonModule,
   MdCardModule
   ],
  providers: [],
  exports:[ MdCardModule ] <== this line
})
export class TaskModule{}

Like this the second module imports the first module which imports and exports the component.

When we import the module in the second module we need to export it again. Now we can use the first component in the second module.

Violist answered 18/8, 2018 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.