Angular 2 : export component on a module and import and use it inside a module
28,820
because you import from module file, you could do something like this.
customer-info.module.ts
import {CustomerInfoComponent} from './customer-info.component';
export {CustomerInfoComponent};
Author by
Prabash B
Updated on March 25, 2020Comments
-
Prabash B about 4 years
I have a feature module called CustomerInfoModule which exports a CustomerInfoComponent. see below.
import {NgModule} from '@angular/core' import {RouterModule} from '@angular/router' import {CustomerInfoComponent} from './customer-info.component' @NgModule({ declarations:[CustomerInfoComponent], exports:[CustomerInfoComponent] }) export class CustomerInfoModule{ }
I want to import and use this CustomerInfoComponent inside MissedCollectionsComponent. I am getting typescript error
'.module"' has no exported member 'CustomerInfoComponent'
.
import {NgModule} from '@angular/core' import {RouterModule} from '@angular/router' import {MissedCollectionsComponent} from './missed-collections.component' import {CustomerInfoComponent} from '../shared/customer/customer-info.module' @NgModule({ imports:[RouterModule.forChild([ {path:'missedcollection',component:MissedCollectionsComponent}, {path:'missedcollection/customerinfo',component:CustomerInfoComponent} ]), CustomerInfoModule], declarations:[], exports:[] }) export class MissedCollectionsModule{ }
As per the Angular2 documentation it says:
'We export the ContactComponent so other modules that import the ContactModule can include it in their component templates.' link
Is it not logical to import componets from a module and use it inside another module. Am I wrong thinking that/or missing someting?
-
Prabash B over 7 yearsYes that would work. Thanks. But my concern is when we export a component why its not visible to import from a another module.
-
Tiep Phan over 7 years@PrabashB note that,
exports
in NgModule mean, you do not needdeclarations
some Components, Directives, Pipes if youimports
other NgModule alreadydeclarations
them. -
Prabash B over 7 yearsthanks. it worked. small correction to your code. Below code worked for me.import {CustomerInfoComponent} from './customer-info.component' export {CustomerInfoComponent} Please edit your code. so then I will accept it .
-
Prabash B over 7 yearsI want to make my CustomerInfoComponent encapsulated in the CustomerInfoModule. Therefore second option of importing file component directly would not be accepted as the anwer here.
-
challamzinniagroup about 4 yearsA more complete answer would be much better here. Offering a 2-line code snippet with no real explanation isn't particularly helpful.