微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular 9 ngOnChange:SimplesChanges.firstChange始终为false,但值已更改

如何解决Angular 9 ngOnChange:SimplesChanges.firstChange始终为false,但值已更改

我有这个组成部分。我想在@Input第一次更改时做一些事情。问题是changes.firstChange始终为假(在初始化和更改后)。

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,selector: 'test',templateUrl: './t.component.html',styleUrls: ['./t.component.css']
})
export class TComponent implements OnChanges {

  @input()
  myValue: string;

  ngOnChanges(changes: SimpleChanges) {
    console.log('myValue:  ',changes.myValue.isFirstChange()); // always false
    console.log('myValue:  ',changes);
  }

}

为了使用它,我正在做

  myValue = of('1').pipe(
    delay(2000),// startWith('2')
   )

我也尝试过:

  myValue = '1'

<test [myValue]="myValue | async "></test> //使用of

正如您在图像中看到的,changes.firstChange始终为假,无论以后再传递字符串还是新值。

这些是输出

enter image description here

enter image description here

这是使用rxjs值时的输出:(值已更改但从未更改,请先更改为true

enter image description here

解决方法

因为没有之前的值

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。