问题
This is my code:
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { LanguageTranslationModule } from './shared/modules/language-translation/language-translation.module'
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AuthGuard } from './shared';
import { SidebarComponent } from './layout/components/sidebar/sidebar.component';
import { HeaderComponent } from './layout/components/header/header.component';
@NgModule({
imports: [
CommonModule,
BrowserAnimationsModule,
HttpClientModule,
LanguageTranslationModule,
AppRoutingModule
],
declarations: [AppComponent,HeaderComponent,SidebarComponent],
providers: [AuthGuard],
bootstrap: [AppComponent],
exports: [
HeaderComponent,
SidebarComponent
],
})
export class AppModule {}
I don't why I obtain this excepion:
Error: BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import CommonModule instead. Error: BrowserModule has already been loaded. If you need access to common directives such as NgIf and NgFor from a lazy loaded module, import
In the future module I import CommonModule
and not BrowerModule
. Anyone can help me?
回答1:
Import BrowserAnimationsModule
and HttpModule
only once (either in your root module or a core module).
import these mentioned modules only once(in app-module only):
BrowserModule
, BrowserAnimationsModule
, LazyLoadImageModule
(if using it), CarouselModule
(if using it), InfiniteScrollModule
(if using it), HttpModule
( if using it)
回答2:
Import BrowserAnimationsModule in AppModule only because BrowserAnimationsModule by default import BrowserModule, No Need to define BrowserModule in app.module.ts and then import CommonModule in your child / featured module.
@NgModule({
imports: [
BrowserAnimationsModule
]
})
export class ParentModule {}
Featured / Child Module
@NgModule({
imports: [ CommonModule ]
})
export class FeaturedModule {}
Hope, this solves your problem
回答3:
Replace CommonModule with BrowserModule in your AppModule.
import { BrowserModule} from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { LanguageTranslationModule } from './shared/modules/language-translation/language-translation.module'
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AuthGuard } from './shared';
import { SidebarComponent } from './layout/components/sidebar/sidebar.component';
import { HeaderComponent } from './layout/components/header/header.component';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
LanguageTranslationModule,
AppRoutingModule
],
declarations: [AppComponent,HeaderComponent,SidebarComponent],
providers: [AuthGuard],
bootstrap: [AppComponent],
exports: [
HeaderComponent,
SidebarComponent
],
})
export class AppModule {}
来源:https://stackoverflow.com/questions/56110455/browsermodule-has-already-been-loaded