延迟加载BrowserModule已加载

时间:2017-08-31 07:27:45

标签: angular typescript angular2-routing lazy-loading angular2-modules

我正在尝试实现延迟加载但是会出现以下错误 **

  

错误错误:未捕获(在承诺中):错误:BrowserModule已经存在   已装载。如果您需要访问常用指令,如NgIf和   来自延迟加载模块的NgFor,改为导入CommonModule。

**

需要帮助。 这是我的模块

  1. 共享媒体
  2. @NgModule({
    
      declarations: [TimePipe],
      providers: [
        EmsEmployeeService,
        EmsDesignationService,
        EmsOrganizationService,
        EmsAuthService,
        AmsEmployeeService,
        AmsShiftService,
        ValidatorService,
        AmsLeaveService,
        AmsAttendanceService,
        AmsDeviceService,
        AmsOrganizationService,
        AmsAlertService,
        AmsHolidayService,
        AutoCompleteService,
        AmsTimelogsService,
        LocalStorageService
      ],
      imports: [
        HttpModule,
        ToastyModule.forRoot(),
        AgmCoreModule.forRoot({
          apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
        }),
      ],
      exports: [
        FormsModule,
        HttpModule,
        BrowserAnimationsModule,
        RouterModule,
        MaterialModule,
        MdDatepickerModule,
        MdNativeDateModule,
        ToastyModule,
        FileUploadModule,
        NgxPaginationModule,
        NguiAutoCompleteModule,
        AgmCoreModule,
        TimePipe
      ]
    })
    export class SharedModule { }
    

    2.SettingModule

     @NgModule({
      imports: [
        CommonModule,
        SharedModule,
        SettingsRoutingModule
      ],
      declarations: [
        SettingsComponent,
        ShiftsComponent,
        DevicesComponent,
        AlertsComponent,
        HolidaysComponent,
        AlterTypesComponent,
        AlterEditComponent,
        ShiftTypeNewComponent,
        DeviceLogsComponent,
        ChannelTypesComponent,
        ChannelTypeEditComponent
      ], exports: [
        SettingsComponent,
        ShiftsComponent,
        DevicesComponent,
        AlertsComponent,
        HolidaysComponent,
        AlterTypesComponent,
        AlterEditComponent,
        ShiftTypeNewComponent,
        DeviceLogsComponent,
        ChannelTypesComponent,
        ChannelTypeEditComponent,
      ]
    })
    export class SettingsModule { }
    
    3.SettingRoutingModule
    
    const settings_routes: Routes = [
      { path: '', redirectTo: 'shifts', pathMatch: 'full' },
      { path: 'shifts', component: ShiftsComponent },
      { path: 'shifts/new', component: ShiftTypeNewComponent },
      { path: 'shifts/edit/:id', component: ShiftTypeNewComponent },
      { path: 'devices', component: DevicesComponent },
      { path: 'deviceLogs', component: DeviceLogsComponent },
      { path: 'holidays', component: HolidaysComponent },
      { path: 'alerts', component: AlertsComponent },
      { path: 'alerts/types', component: AlterTypesComponent },
      { path: 'alerts/:id', component: AlterEditComponent },
      { path: 'channelTypes', component: ChannelTypesComponent },
      { path: 'channelTypes/:id', component: ChannelTypeEditComponent }
    ];
    
    
    const routes: Routes = [
      { path: '', component: SettingsComponent, children: settings_routes }
    ];
    
    
    
    @NgModule({
      imports: [RouterModule.forChild(routes)],
      exports: [RouterModule]
    })
    export class SettingsRoutingModule { }
    
    1. 应用的路由模块
    2. const attdendance_routes: Routes = [
        { path: '', redirectTo: 'daily', pathMatch: 'full' },
        { path: 'monthly', component: MonthlyComponent },
        { path: 'daily', component: DailyComponent },
      
        { path: 'daily/:empId', component: AttendanceDetailsComponent },
        { path: 'details/:empId', component: AttendanceDetailsComponent },
        { path: 'monthly/:empId', component: AttendanceDetailsComponent },
        { path: 'leaves/:empId', component: AttendanceDetailsComponent },
      
        { path: 'details/:empId/apply-leave', component: ApplyLeaveComponent },
        { path: 'daily/:empId/apply-leave', component: ApplyLeaveComponent },
        { path: 'daily/:empId/attendance-logs/:ofDate', component: AttendanceLogsComponent },
        { path: 'monthly/:empId/apply-leave', component: ApplyLeaveComponent },
        { path: 'leaves/:empId/apply-leave', component: ApplyLeaveComponent },
        { path: 'leaves/new/apply', component: ApplyLeaveComponent },
      
        { path: 'leaves', component: LeavesComponent },
        { path: 'leave-balances', component: LeaveBalancesComponent },
        { path: 'leave-balances/:empId', component: AttendanceDetailsComponent },
        { path: 'manage-leaves', component: ManageLeavesComponent },
      
      ];
      
      
      
      const emp_routes: Routes = [
        { path: '', redirectTo: 'list', pathMatch: 'full' },
        { path: 'list', component: EmployeeListComponent },
        { path: 'list/:id', component: EmpEditComponent },
        { path: 'designations', component: DesignationsComponent }
      ];
      
      
      
      const page_routes: Routes = [
        { path: '', redirectTo: 'attendances', pathMatch: 'full' },
        { path: 'employees', component: EmployeesComponent, children: emp_routes },
        { path: 'attendances', component: AttendancesComponent, children: attdendance_routes },
      
        { path: 'settings', loadChildren: './pages/settings/settings.module#SettingsModule' },
      ];
      
      // main routes
      const routes: Routes = [
        { path: '', redirectTo: 'pages', pathMatch: 'full' },
        { path: 'login', component: LoginComponent, canActivate: [LoginGuard] },
        { path: 'pages', component: PagesComponent, canActivate: [UserGuard], children: page_routes },
        { path: 'loginViaOrg', component: OrgLoginComponent },
        { path: 'download', component: AppDownloadComponent },
        { path: '**', redirectTo: 'pages' },
      ];
      
      @NgModule({
        imports: [RouterModule.forRoot(routes, { useHash: true })],
        exports: [RouterModule]
      })
      export class AppRoutingModule { }
      

      5.AppModule

      @NgModule({
      
        declarations: [
          AppComponent,
          PagesComponent,
          LoginComponent,
          EmployeesComponent,
          OrgLoginComponent,
          EmployeeListComponent,
          EmpEditComponent,
          DayEventDialogComponent,
          AttendancesComponent,
          MonthlyComponent,
          AttendanceDetailsComponent,
          DailyComponent,
          DeviceDialogComponent,
          LeaveActionDialogComponent,
          LeavesComponent,
          LeaveBalancesComponent,
          ManageLeavesComponent,
          ApplyLeaveComponent,
          ConfirmDialogComponent,
          ResetPasswordDialogComponent,
          AppDownloadComponent,
          DesignationsComponent,
          AttendanceLogsComponent,
        ],
      
        entryComponents: [
          DayEventDialogComponent,
          DeviceDialogComponent,
          LeaveActionDialogComponent,
          ConfirmDialogComponent,
          ResetPasswordDialogComponent
        ],
      
        imports: [
          BrowserModule,
          // CommonModule,
          SharedModule,
          AppRoutingModule,
          // feature modules
          // SettingsModule
        ],
      
        providers: [
          LoginGuard, UserGuard,
        ],
      
        bootstrap: [AppComponent]
      })
      export class AppModule { }
      

12 个答案:

答案 0 :(得分:23)

仅导入BrowserAnimationsModuleHttpModule (在根模块或核心模块中)。

答案 1 :(得分:3)

我也得到了同样的错误,最后,经过一点点的挣扎,我能够解决它。

仅导入这些提到的模块一次(仅限app-module):

BrowserModule, BrowserAnimationsModule, LazyLoadImageModule(如果使用它), CarouselModule(如果使用它), InfiniteScrollModule(如果使用它), HttpModule(如果使用它)

答案 2 :(得分:2)

如果您在某些child.app module.ts中导入了BrowseModule,则会发生此错误。请确保在app.module以外的所有模块中导入 CommonModule ,因为它有浏览器模块。

答案 3 :(得分:1)

我遇到了同样的问题, Jota.Toledo 给出了正确答案,我只想扩展一下:请检查共享模块导入中与

相关的所有模块
  

@ angular / platform-b​​rowser / animations

并将这些模块移至app.module.ts

答案 4 :(得分:1)

您需要检查应用程序的其他部分是否正在调用或导入 BrowserModule。有些库使用 BrowserModule,这样做会破坏 BrowserModule。

答案 5 :(得分:0)

@First Import BrowerModule and in imports also first include BrowserModule import:[BrowerModule ] 

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

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

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

如果我们在应用BrowserModule以外的任何模块中声明module(Duplicate),则会出现此错误。在应用程序模块中,如果我们要导入10个模块或插件,则首先我们必须在顶部导入BrowserModule并在顶部声明装饰(import:[BrowserModule ])

答案 6 :(得分:0)

仅在父组件中(或仅在appModule中)包括所有通用模块。在子模块中,仅包括子模块。

此外,您可能需要添加 schemas: [NO_ERRORS_SCHEMA],在您的子模块和父模块中

答案 7 :(得分:0)

如果您使用多个模块,请在应用程序模块或某些自定义模块中仅使用Browser模块一次,并在自定义模块中使用CommonModule中的@angular/common

我遇到了同样的错误,我试图在多个组件/模块中重用组件,指令,管道。取而代之的是,我将所有可重复使用的组件导入一个核心模块,并将该核心模块导入多个组件/模块。

答案 8 :(得分:0)

就我而言,我有一个共享模块,该模块正在导入BrowserAnimationModule。我在根模块中导入了共享模块。要解决此错误,请从所有模块中删除所有导入的BrowserAnimationModule并将其添加到根模块中。

imports: [
    AppRoutingModule,
    SharedModule,
    BrowserAnimationsModule
  ],

答案 9 :(得分:0)

我通过从共享模块中删除 BreadcrumbsModule 解决了这个问题。

答案 10 :(得分:0)

这是我修复它的方式:

我添加到项目中的惰性模块也包含 BrowserModule

那时我从模块中删除了它,然后它就起作用了。

答案 11 :(得分:0)

注意:root中必须有一个“BrowserModule”,并且每个子组件必须有一个“CommonModule”