angular - 如何在angular2中使用sharedModule?

标签 angular typescript angular2-modules ng-modules

我有一个 angular2 组件,我想在多个模块之间共享它。

所以我在下面写了 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 { }

然后我将这个 sharedModule 添加到多个模块,如下所示:

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 { }

我也同样将 sharedModule 添加到 generic.module.ts,

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 { }

但是当我尝试在 generic.module.ts 的组件中使用通用组件时出现以下错误

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

最佳答案

这是我的应用程序使用共享模块的代码:

应用模块:

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 {} 

主页模块:

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 {} 

和共享模块:

@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 ]
    };
  }
}

我的应用程序中有 20 多个模块,我在所有地方都使用共享模块。这很好用。希望对您有所帮助。

关于angular - 如何在angular2中使用sharedModule?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630917/

相关文章:

javascript - typescript :将方法添加到类的数组

angular - 嵌套模块在 Angular 中不好吗?

angular - 在 angular2 中的延迟加载模块之间共享一个外部模块

angular - 如何从 Angular 属性绑定(bind)加载框架资源

angular - Dragula 拖放复制 ng2 的一种方式

angular - 路由和模块

angular - 我在哪里可以获得 ngModule 中所有可能的 angular2 导入的列表?

css - 删除 Material 对话框上的怪异边框

带有数据的 Angular 嵌套组件

typescript - 如何推断泛型参数的属性类型并映射到另一种类型