无法在NgModule中声明模块,因为它不是当前编译的一部分。Angular

42

我不确定这里发生了什么,但是在将'FormsModule'和'reactiveformsmodule'导入到我的'app.module'组件时,我收到了一个错误消息“Cannot declare 'FormsModule' in an NgModule as it's not a part of the current compilation”。 我正在运行Angular 9,并尝试清除缓存、重新安装npm。这可能是Angular 9的问题吗?降级到8会解决问题吗......

我对这里发生的事情感到困惑,因为前一天导入时它很好地工作了。

app.module.ts

    import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { ExpertAdviceLandingPageComponent } from './expert-advice-landing-page/expert-advice-landing-page.component';
import { InstagramComponent } from './instagram/instagram.component';
import { MDBBootstrapModule } from 'angular-bootstrap-md';
import { SharedModule } from './shared/shared.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BuyingGuideComponent } from './buying-guide/buying-guide.component';
import { IdeasAndInspirationComponent } from './ideas-and-inspiration/ideas-and-inspiration.component';
import { AdviceAndGuidanceComponent } from './advice-and-guidance/advice-and-guidance.component';
import { GuidesComponent } from './guides/guides.component';
import { CommonModule } from '@angular/common';
import { CarouselComponent } from './carousel/carousel.component';
import { CarouselModule } from 'angular-bootstrap-md';
import { AppRoutingModule } from './app-routing.module';
import { SharedService } from './shared/sharedService';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    BuyingGuideComponent,
    IdeasAndInspirationComponent,
    AdviceAndGuidanceComponent,
    GuidesComponent,
    ExpertAdviceLandingPageComponent,
    InstagramComponent,
    CarouselComponent,
    FormsModule,
    ReactiveFormsModule
  ],
  imports: [
    BrowserModule,
    SharedModule,
    HttpClientModule,
    CommonModule,
    CarouselModule,
    MDBBootstrapModule.forRoot(),
    BrowserAnimationsModule,
    AppRoutingModule,
    NgbModule,
    FormsModule,
    ReactiveFormsModule

  ],
  providers: [SharedService],
  bootstrap: [AppComponent]
})
export class AppModule {
}

3
考虑从declarations数组中移除FormsModuleReactiveFormsModule - Ashish Ranjan
1
哦,天啊,我甚至没有注意到那个!谢谢你。 - ashley g
3个回答

42

在Angular中导入模块应该放在imports数组下,而不是declarations

declarations: [
    AppComponent,
    // ReactiveFormsModule---remove from declarations and add in import because its is imported
    // FormsModule`enter code here`---remove from declarations and add in import
],
imports: [
    ReactiveFormsModule,
    FormsModule
]

6

在Angular中导入模块时,应将其放置在imports数组下而不是declarations下。

@NgModule({
declarations: [
     AppComponent
],
imports: [
     BrowserModule,
     AppRoutingModule, 
     MatSliderModule    // whatever module you want to use
],
providers: [],
bootstrap: [AppComponent]
});

0

如果你在库中工作并从错误的源导入模块,也可能会发生错误。

import { myLibraryModule } from '@my/library'

应该是这样的

import { myLibraryModule } from './library.module'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接