【发布时间】:2022-06-20 02:18:35
【问题描述】:
从 Angular 11 迁移到 Angular 12 并弹出此问题:
“错误:双向绑定'value'的属性和事件半部没有绑定到同一个目标。”
父页面组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
<app-child [(value)]="counterValue"></app-child>
`,
styles: []
})
export class ParentComponent {
counterValue = 0;
}
子组件:
import { Component, OnInit, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'app-child',
template: `
{{ value }}
`,
styles: []
})
export class ChildComponent {
@Input() value = 0;
@Output() incrementValue = new EventEmitter<number>();
increase(): void {
this.value++;
this.incrementValue.emit(this.value);
}
}
这里有一个stackblitz 代码供您自己测试:
这是一个错误吗? 还是我错过了什么?
【问题讨论】:
-
发射器应该被称为
valueChange(属性+更改)参见docs。你的forked stackblitz
标签: angular typescript angular12 two-way-binding