繁体   English   中英

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

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

我正在写一个 todolist 演示。 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>
   ~~

也没有检查所有项目。(即使他们的 isDone 状态是真的)

我在 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 如下。

 <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>

谁能看到我做错了什么? 谢谢!

当我收到此错误时,是因为我忘记在FormsModule中导入app.module.ts 所以在 app.module.ts 中:

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

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

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

在这里,ngModule 无法识别..

就我而言,这是因为我制作了自己的组件,并且没有定义 output 事件发射器 (visibleChange)。

突出显示发射器的代码示例:

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();
}

}

在以下情况下也可能发生此错误:

  • 您在“外部”模块中使用 CUSTOM_ELEMENTS_SCHEMA
  • 您正在绑定到另一个模块的组件
  • 并且您没有将要绑定的组件作为声明该组件的模块的导出的一部分。

解决方法是从其声明模块中导出组件。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM