Angular 数组-更改不会传播到子组件
家长:Angular 数组-更改不会传播到子组件,angular,typescript,Angular,Typescript,家长: <div id="join-container"> <join-chain id="my-join-chain" [selectedColumn]="selectedColumn" (updatedStatements)=onUpdatedStatements($event)> </join-chain> <tile-canvas id="my-tile-canvas" [mystate
<div id="join-container">
<join-chain
id="my-join-chain"
[selectedColumn]="selectedColumn"
(updatedStatements)=onUpdatedStatements($event)>
</join-chain>
<tile-canvas
id="my-tile-canvas"
[mystatements]="theStatements"
(selectedColumn)="onSelectedColumn($event)">
</tile-canvas>
</div>
孩子:
...
mappings: Mapping[] = []
@Input() set mystatements(someStatements: Relational.JoinStatement[] | undefined) {
console.log(">> tile canvas interceptor")
console.log(someStatements)
if (someStatements !== undefined) {
let idsFromStatements = someStatements
.map(statement => [statement.leftTile, statement.rightTile])
.filter(mapping => (mapping[0] !== undefined) && (mapping[1] !== undefined)) as ([Relational.tileId, Relational.tileId])[]
this.mappings = idsFromStatements.map(ids => new Mapping(ids))
}
}
当我玩应用程序时,我看到从步骤加入触发的消息
。
另一方面,>tile canvas interceptor
只发生一次,发生在从步骤加入启动的第一个之后
我读了好几篇文章,发现有一个类似的模式,那就是每次都会触发“拦截器”
onUpdatedStatements(statements: Relational.JoinStatement[]) {
this.theStatements = statements;
console.log(">> fired from step-joining")
console.log(this.theStatements)
}
If语句具有与此相同的引用和值。更改后的语句将不会从控制台在子组件中激发。log
我看到语句更改。想知道我们有一个数组这一事实是否会把事情搞砸。检查这个答案是的,我现在理解这个问题了。为了触发更改,您将如何处理对象数组?谢谢@NoIdeaHowToFixThis-您可以尝试this.theStatements=statements.slice(0)代码>。这将创建源数组的副本。请考虑创建StasBLITIZ。
onUpdatedStatements(statements: Relational.JoinStatement[]) {
this.theStatements = statements;
console.log(">> fired from step-joining")
console.log(this.theStatements)
}