4

I have one angular2 component which I want to share among multiple modules.

So I wrote below sharedModule ,

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {GenericComponent} from './generic/generic.component';
@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ GenericComponent ],
  exports:      [ GenericComponent ]
})
export class SharedModule { }

then I added this sharedModule to multiple modules as below:

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {SharedModule} from './shared.module';
import { AppComponent } from './app.component';
@NgModule({
  imports:      [ BrowserModule,SharedModule ],
  declarations: [ AppComponent ],
  exports:      [ AppComponent ],
  bootstrap: [AppComponent]
})
export class AppModule { }

I also added sharedModule to generic.module.ts similarly ,

generic.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {SharedModule} from './shared.module';
@NgModule({
  imports:      [ BrowserModule,SharedModule ],
  declarations: [ //.. here I have added all the components that generic uses ]
})
export class GenericModule { }

but I am getting below error when I am trying to use generic component inside components of generic.module.ts

Unexpected value 'undefined' imported by the module 'GenericModule'

4
  • I don't see that GenericModule anywhere.. pls add it. Commented Sep 22, 2016 at 5:34
  • 1
    Are you also importing GenericModule into SharedModule? Ive had this happen before where it caused a failing circular dependency Commented Sep 22, 2016 at 6:18
  • @peeskillet no.. i am importing generic.component inside sharedModule,and I am importing this shared module inside genericModule & AppModule Commented Sep 22, 2016 at 6:20
  • @peeskillet Actually I have this problem. My sharedModule contains a pipe and a module (A), the module A need the pipe of ShareModule, so I import it from within the module A. But I have a error Unexpected value 'undefined' imported by the module 'A'. What I'm suppose to do in this case ? Commented Feb 11, 2017 at 14:02

2 Answers 2

8

Here's the code from my app using a shared module:

App module:

import { AboutModule } from './about/about.module';
import { SharedModule }   from './shared/shared.module';
import { Menubar, MenuItem } from 'primeng/primeng';

@NgModule({
    imports: [ BrowserModule, RouterModule.forRoot(appRoutes), SharedModule.forRoot(), 
               HomeModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ],
    providers: [ appRoutingProviders ]

}) 

export class AppModule {} 

The home module:

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


@NgModule({
    imports: [ SharedModule, routing],
    declarations: [ HomeComponent, LoginComponent, RegisterComponent, VerifyComponent, 
                   VerifyEmailComponent, ForgotComponent, ForgotVerifyComponent, 
                   ChangeComponent, ChallengeComponent, LogoutComponent ], 
    bootstrap: [ HomeComponent ],
    providers: [ ]

}) 

export class HomeModule {} 

And the shared module:

@NgModule({
  imports: [CommonModule, RouterModule, MenubarModule, GalleriaModule, InputTextModule, PanelModule, ButtonModule,
            DropdownModule, DialogModule, AccordionModule, CalendarModule, SelectButtonModule, CheckboxModule,
            ProgressBarModule, DataTableModule],
  declarations: [ ErrorMessagesComponent, FoodDashboardComponent ],
  exports: [ CommonModule, ReactiveFormsModule, HttpModule, RouterModule,
            MenubarModule, GalleriaModule, InputTextModule, PanelModule, ButtonModule, DropdownModule, DialogModule, AccordionModule, CalendarModule,
            SelectButtonModule, CheckboxModule, DataTableModule, ProgressBarModule, ErrorMessagesComponent, FoodDashboardComponent ]
})

export class SharedModule {
  //
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [SettingsService, AppMenuService, AuthorizationService, LoginService, RegisterService, ThemeService, ValidationService,
        NutritionixService, AuthGuardService, CalculationService, ChallengeService ]
    };
  }
}

I have more than 20 modules in my app and I use the shared module all over the place. This works just fine. Hope it helps.

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

Comments

4

You can't use BroswerModule in featureModule. So make sure you use CommonModule instead of using BrowswerModule.

import { NgModule }      from '@angular/core';
import { CommonModule }        from '@angular/common';
import {SharedModule} from './shared.module';
@NgModule({
  imports:      [ CommonModule ,SharedModule ],
  declarations: [ //.. here I have added all the components that generic uses ],
  exports:      [ CommonModule]
})
export class GenericModule { }

10 Comments

still getting the error : nexpected value 'undefined' imported by the module 'GenericModule'
i think one of the thing which you are importing was not found. check by removing one by one so that you will get which import gives you error
should I add GenericComponent to declarations inside generic.module too? @ranakrunal9
But you purpose is not clear. is it a lazy module or what? I can't see any connection bet GenericModule and AppModule. Why have you injected sharedModule everywhere? there are so many questions to ask.
I tried importing generic.component inside generic.module too, still not working
|

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.