尝试更新同一组件中的输入值,但无法更新。出现如下错误
ERROR
Error: Cannot read properties of undefined (reading 'pop')
那么,如何解决这个问题呢?
table.component.ts:
export class TableComponent implements OnInit {
@Input() names: any;
constructor() {}
ngOnInit() {}
testFn() {
this.names.pop('Test22');
this.names = [...this.names];
console.log(this.names);
}
}
1条答案
按热度按时间ndasle7k1#
杰·斯瓦米纳拉扬!
在传递组件引用时,您的操作稍有错误。
在ChangeComponent中,它未正确引用表组件。在AppComponent HTML中,表组件必须作为引用输入传递给changecomponent。
您可以查看此更正代码
https://stackblitz.com/edit/angular-pass-table-data-to-input-property-2ehcxs?file=src%2Fapp%2Ftable%2Ftable.component.html,src%2Fapp%2Ftable%2Ftable.component.ts,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fchange%2Fchange.component.html,src%2Fapp%2Fchange%2Fchange.component.ts,src%2Fapp%2Ftable%2Ftable.component.css