Angular 12 - 双向绑定(bind)给出错误 : The property and event halves of the two-way binding 'prop_name' are not bound to the same target

标签 angular typescript angular12 two-way-binding

从 Angular 11 迁移到 Angular 12 并弹出此问题:

错误:双向绑定(bind)“值”的属性和事件部分未绑定(bind)到同一目标。”

父页面组件:

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供您自行测试的代码:

这是一个错误吗? 还是我遗漏了什么?

最佳答案

我只是使用变通方法继续开发,同时解决此问题。

import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `
    <app-child [value]="counterValue" (incrementValue)="onChange($event)"></app-child>
  `,
  styles: []
})
export class ParentComponent {
  counterValue = 0;
  onChange(n:number) {
    this.counterValue = n;
  }
}

关于Angular 12 - 双向绑定(bind)给出错误 : The property and event halves of the two-way binding 'prop_name' are not bound to the same target,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71483441/

相关文章:

javascript - 图表和 x 轴上的高度图极坐标散点图值

angular - 如何修复 npm 审计漏洞 angular 12.0.3

Angular 类型 '{ domain: string[]; }' 不可分配给类型 'string | Color'

pattern-matching - 您如何在 TypeScript 中模拟 ADT 和模式匹配?

css - 如何在 Ionic2 的 Alert 中更改按钮的颜色

TypeScript 接口(interface)和实现

angular - 一个 @ngrx/effect 中的多个 Http 调用

javascript - Angular 2路由器无需重新加载即可更改网址

html - 让顶部和底部边距留在 mat-grid-tile 内?

angular - 在输入更改和更新 View 时调用服务