admin管理员组

文章数量:1306824

I'm new to angular and I was following this hero tutorial when I stumbled upon this error:

Type 'Event' is not assignable to type 'string'

I reproduced the error here.

I'm new to angular and I was following this hero tutorial when I stumbled upon this error:

Type 'Event' is not assignable to type 'string'

I reproduced the error here.

Share Improve this question asked Mar 14, 2021 at 14:31 RasmusRasmus 531 silver badge3 bronze badges 1
  • Hi, can you please add some additional information (e.g. a snippet of the code where the error is being thrown, along with surrounding imports and used declarations) for historical purposes? Links to outside sources can change often and it's helpful if someone es across the same or a similar issue. – 5ar Commented Mar 14, 2021 at 17:56
Add a ment  | 

3 Answers 3

Reset to default 6

You are missing the FormsModule import in your AppModule.

import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { AppComponent } from "./app.ponent";
import { HeroComponent } from "./hero/hero.ponent";

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

if you are working with standalone Components (which is angular 14 new feature ) then you have to import

form module in your ponents Ts file

import { Component, OnInit } from "@angular/core";
import  {CommonModule} from '@angular/mon'

**import { FormsModule } from '@angular/forms';**


@Component({
    standalone:true,
    selector: 'test',
    templateUrl:'./test.ponents.html',
    styleUrls : ['./test.ponents.css'],
    imports:[CommonModule,**FormsModule **]
})

I'm also learning and I was going crazy because I could not follow a course on Angular due to the fact that the sample project from https://stackblitz. does not have a module and relies on standalone ponents. The solution that works on 2023 for those without a module (using only standalone ponents) is to import FormsModule (at the beginning and at the imports section) of both the main.ts file and the ponent where you want to use forms, as both ponents need to be linked (instead of using the module file).

So you just add the import information for FormsModule to the ponents (ts files):

import { Component, OnInit } from "@angular/core";
import  {CommonModule} from '@angular/mon'

    **import { FormsModule } from '@angular/forms';**
    
    
    @Component({
        standalone:true,
        selector: 'test',
        templateUrl:'./test.ponents.html',
        styleUrls : ['./test.ponents.css'],
        imports:[CommonModule,**FormsModule **]

本文标签: javascriptType 39Event39 is not assignable to type 39string39Stack Overflow