【发布时间】:2020-09-29 17:09:25
【问题描述】:
在这里,我试图研究变更检测的工作原理。使用更改检测OnPush 在父组件和子组件中具有数组(子项)。试图通过拼接或推送来改变数组,仍然影响子组件模板。即使@Input 的引用没有改变,这怎么行?
我的父组件.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
children = [1,2,3,4,5,6,7,8,9]
emit(j) {
this.children.splice(j, 1);
}
}
父级html
<div>
<app-multi [input]="children" (output)="emit($event)"></app-multi>
</div>
我的子组件.ts
@Component({
selector: 'app-multi',
templateUrl: './multi.component.html',
styleUrls: ['./multi.component.css'],
changeDetection:ChangeDetectionStrategy.OnPush
})
export class MultiComponent implements OnInit,OnChanges,DoCheck,AfterViewInit{
@Input('input') ele = [];
data = [];
@Output('output') outputEmi = new EventEmitter();
ngOnInit() {
this.data = this.ele;
}
clicks(value){
this.outputEmi.emit(value);
}
}
子html
{{name.name}}
<table>
<tbody>
<tr>
<td *ngFor="let head of data; let i = index;">
<span>{{head}}</span>
<button class="delete" (click)="clicks(i)">x</button>
</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: angular typescript