【发布时间】:2020-12-22 11:47:01
【问题描述】:
我有一个从另一个组件更新的数组(更新正在发生,字符串被添加到我用测试按钮检查过的数组中)但 ngOnChanges 不会检测到任何更改。我的代码有什么问题?
我应用的更改是 array.push()。
import { Component, OnInit, Input, OnChanges } from '@angular/core';
import {MatAccordion} from '@angular/material/expansion';
@Component({
selector: 'app-cart',
templateUrl: './cart.component.html',
styleUrls: ['./cart.component.css']
})
export class CartComponent implements OnInit, OnChanges {
totalPrice = 0;
constructor() { }
@Input() Products: Array<String>;
ngOnChanges() {
console.log(this.Products)
}
ngOnInit(): void {
}
}
【问题讨论】:
-
你有 ChangeDetectionStrategy.OnPush 在这个组件的父组件的某处吗?
-
这个链接是关于对象这是一个数组
-
链接中没有提到你提到的策略
-
第一个评论是别人写的,数组和对象在这方面表现一样
标签: angular typescript lifecycle ngonchanges