简体   繁体   English

错误 TS2322:类型“事件”不可分配给类型“布尔值”

[英]error TS2322: Type 'Event' is not assignable to type 'boolean'

I'm writing a todolist demo.我正在写一个 todolist 演示。 When ng serve it, it shows an error:ng serve它时,它显示错误:

Error: src/app/app.component.html:17:58 - error TS2322: Type 'Event' is not assignable to type 'boolean'.
17  <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" >
                                                       ~~~~~~~~~~~~~~
18 
19  <label>{{ todo.title }}</label>
   ~~

Also all items not be checked.(even their isDone status is true)也没有检查所有项目。(即使他们的 isDone 状态是真的)

I def an object in app.component.ts.我在 app.component.ts 中定义了一个 object。

 public todos:{ id:number, title: string, isDone: boolean }[]=todos const todos=[ { id:1, title:'study', isDone:true },{ id:2, title:'sleep', isDone:true },{ id:3, title:'drink', isDone:true } ]

app.component.html as below. app.component.html 如下。

 <li *ngFor="let todo of todos"> <div class="view"> <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" > <label>{{ todo.title }}</label> <button class="destroy"></button> </div> <input class="edit" value="Create a TodoMVC template"> </li>

Can anyone see what I'm doing wrong?谁能看到我做错了什么? thanks!谢谢!

When I got this error, it was because I forgot to import the FormsModule in app.module.ts .当我收到此错误时,是因为我忘记在FormsModule中导入app.module.ts So in app.module.ts:所以在 app.module.ts 中:

import { FormsModule } from '@angular/forms';
...
imports: [
..,
FormsModule
],

change you code from <input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" > to this...将您的代码从<input class="toggle" type="checkbox" [(ngModule)]="todo.isDone" >更改为...

<input class="toggle" type="checkbox" [(ngModel)]="todo.isDone" >

Here, ngModule is not recognizing..在这里,ngModule 无法识别..

In my case it was because I had made my own component and it didn't have the output event emitter (visibleChange) defined.就我而言,这是因为我制作了自己的组件,并且没有定义 output 事件发射器 (visibleChange)。

Code sample with emitter highlighted:突出显示发射器的代码示例:

export class DialogComponent {
@Input() header = '';
@Input() message = '';

@Input()
get visible(): boolean { return this._visible; }
set visible(value: boolean) {
    this._visible = value;
}
private _visible = false;

@Output() visibleChange = new EventEmitter<boolean>();

@Output() closeDialog = new EventEmitter<void>();

onCloseDialog(): void {
    this.closeDialog.emit();
}

} }

This error can also happen in the following situation:在以下情况下也可能发生此错误:

  • you are using CUSTOM_ELEMENTS_SCHEMA in your "outer" module您在“外部”模块中使用 CUSTOM_ELEMENTS_SCHEMA
  • you are binding to a component from another module您正在绑定到另一个模块的组件
  • and you do not have the component to which you are binding as part of the exports from the module declaring the component.并且您没有将要绑定的组件作为声明该组件的模块的导出的一部分。

The fix is to export the component from its declaring module.解决方法是从其声明模块中导出组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ng-particles 时 Angular 中的错误 TS2322 错误“类型'字符串'不可分配给类型'”画布“” - error TS2322 in Angular with error “Type 'string' is not assignable to type '”canvas“” when using ng-particles 错误 TS2322:类型“可观察”<any> ' 不可分配给类型 'string | 细绳[]'。 类型'可观察的<any> ' 缺少以下属性</any></any> - error TS2322: Type 'Observable<any>' is not assignable to type 'string | string[]'. Type 'Observable<any>' is missing the following properties 类型 'HTMLButtonElement' 不可分配给类型 'string'.ts(2322) - Type 'HTMLButtonElement' is not assignable to type 'string'.ts(2322) 类型“数字”不可分配给类型“字符串”.ts(2322) - Type 'number' is not assignable to type 'string'.ts(2322) 类型“事件”不可分配给类型“字符串” - Type 'Event' is not assignable to type 'string' 类型&#39;()=&gt; JQuery&#39;的参数不能分配给类型&#39;()=&gt; boolean&#39;的参数 - Argument of type '() => JQuery' is not assignable to parameter of type '() => boolean' TS2345:“HTMLElement”类型的参数不可分配给“ChartItem”类型的参数 - TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem' '(fg: FormGroup) => { notmatched: boolean; 类型的参数 }' 不可分配给类型的参数 - Argument of type '(fg: FormGroup) => { notmatched: boolean; }' is not assignable to parameter of type React Bootstrap 的 ... 使用 typescript 给出 ts2322 警告 - React Bootstrap's <Col align='center'>…</Col> gives ts2322 warning with typescript 类型“Element”上不存在属性“src”。ts(2339) 和类型“string | null' 不可分配给类型 'string' - Property 'src' does not exist on type 'Element'.ts(2339) and Type 'string | null' is not assignable to type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM