Uncaught Error: Unexpected module 'FormsModule' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation

后端 未结 4 982
说谎
说谎 2021-01-30 19:26

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          


        
相关标签:
4条回答
  • 2021-01-30 19:41

    Add FormsModule in Imports Array.
    i.e

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

    Or this can be done without using [(ngModel)] by using

    <input [value]='hero.name' (input)='hero.name=$event.target.value' placeholder="name">
    

    instead of

    <input [(ngModel)]="hero.name" placeholder="Name">
    
    0 讨论(0)
  • 2021-01-30 19:47

    Things you can add to declarations: [] in modules

    • Pipe
    • Directive
    • Component

    Pro Tip: The error message explains it - Please add a @Pipe/@Directive/@Component annotation.

    0 讨论(0)
  • 2021-01-30 19:57

    FormsModule should be added at imports array not declarations array.

    • imports array is for importing modules such as BrowserModule, FormsModule, HttpModule
    • declarations array is for your Components, Pipes, Directives

    refer below change:

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        FormsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    
    0 讨论(0)
  • 2021-01-30 19:57

    Remove the FormsModule from Declaration:[] and Add the FormsModule in imports:[]

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        FormsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    
    
    0 讨论(0)
提交回复
热议问题