【问题标题】:IntelliJ IDEA & Typescript: Strange behaviour renaming EventEmitterIntelliJ IDEA & Typescript:重命名 EventEmitter 的奇怪行为
【发布时间】:2017-11-08 17:14:45
【问题描述】:

我有一个 Angular2 项目(IntelliJ IDEA),我根据this 示例使用双向绑定:

child.component.ts:

  counterValue = 0;
  @Output() counterChange = new EventEmitter();

  @Input()
  get counter() {
    return this.counterValue;
  }

  set counter(val) {
    this.counterValue = val;
    this.counterChange.emit(this.counterValue);
  }

  decrement() {
    this.counter--;
  }

  increment() {
    this.counter++;
  }

child.component.html:

<button (click)="decrement()">-</button>
<span>{{counter}}</span>
<button (click)="increment()">+</button>

parent.component.html:

<child-component [(counter)]="co"></child-component>
co: {{co}}

parent.component.ts:

co = 4;

代码按预期工作 - 除非我更改一些变量名称(两种方式:使用重构以及手动 - 查找所有引用)。

我可以更改 counterValue 的名称(甚至通过整个代码更改变量类型),但只要重构 counterChange(据我所知,出现 2 次)或 counter(...)(4 in child.component.ts,child.component.html 中的 2,parent.html 中的 1)与父级的输出绑定停止工作。然而,子组件总是按预期工作;只是父组件的 co 变量不再更新。

我重启了 IntelliJ 和 npm,清除了 IntelliJ 和浏览器的缓存:没有帮助。

通过将名称改回示例中的名称,输出绑定再次起作用。

我现在的想法已经不多了,并且有一种忽略一些基本事物的可怕感觉......

【问题讨论】:

  • 请告诉我们更改后的变量名称是什么样的?我怀疑知道错误可能是什么;)
  • 我尝试了从temp 到onTest 到visible 和onVisibilityChange 的几十种变体(主要目的:模仿对话的可见性行为)。在我凭直觉尝试以下方法之前,没有任何效果:发射器必须是后缀为 Change 的 getter 和 setter 函数名。所以cat123 作为getter 和setter 与cat123Change 一起工作......
  • 是的,这就是为什么我要求更改变量名,输出需要是输入变量名+后缀Change
  • 好的。所以这确实是基本的(而且 - 老实说 - 有点烦人)。有了这些信息,我发现一些其他线程指向相同的错误,例如这个:Angular 2 two way data binding not working。非常感谢,伙计们!你让我免于发疯。

标签: angular intellij-idea


【解决方案1】:

你必须记住的一个条件是@output变量名总是输入一+“change”;

例如

@Input() newName: number;
@Output() newNameChange = new EventEmitter();

【讨论】:

猜你喜欢
  • 2016-04-19
  • 2015-06-13
  • 2018-01-17
  • 1970-01-01
  • 2014-02-06
  • 2018-01-23
  • 2016-08-17
  • 2015-05-30
  • 2012-08-26
相关资源
最近更新 更多