本文介绍了未捕获的错误:模块'AppModule'声明了意外的模块'FormsModule'.请添加@ Pipe/@ Directive/@ Component批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular的新手.我开始了《英雄之旅》以学习它.因此,我创建了一个具有two-way绑定的app.component.

I'm new in Angular. I started Tour of Heroes to learn it.So, I am created an app.component with two-way binding.

import { Component } from '@angular/core';
export class Hero {
    id: number;
    name: string;
}
@Component({
    selector: 'app-root',
    template: `
        <h1>{{title}}</h1>
        <h2>{{hero.name}}  details!</h2>
        <div><label>id: </label>{{hero.id}}</div>
        <div><label>Name: </label>
            <input [(ngModel)]="hero.name" placeholder="Name">
        </div>
    `,
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'Tour of Heroes';
    hero: Hero = {
        id: 1,
        name: 'Windstorm'
    };
}

在学习完本教程之后,我导入了FormsModule并将其添加到声明数组中.错误出现在此步骤:

Following the tutorial I imported FormsModule and added it into declarations array. Error appeared at this step:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';

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

这是错误:

推荐答案

FormsModule应该添加到imports array而不是declarations array.

  • 导入数组用于导入诸如BrowserModuleFormsModuleHttpModule
  • 之类的模块
  • 声明数组适用于您的ComponentsPipesDirectives
  • imports array is for importing modules such as BrowserModule, FormsModule, HttpModule
  • declarations array is for your Components, Pipes, Directives

请参考以下更改:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

这篇关于未捕获的错误:模块'AppModule'声明了意外的模块'FormsModule'.请添加@ Pipe/@ Directive/@ Component批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 02:07