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

解决方法

因为没有之前的值