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
3 Answers
Reset to default 6You 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
版权声明:本文标题:javascript - Type 'Event' is not assignable to type 'string' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741827128a2399711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论