我在父组件中有一个这样的变量:
父组件
export class ParentComponent {
variable = {};
varExample = false;
@ViewChild('child') child: ChildComponent;
someFunction () {
this.variable['id'] = {};
for (let i = 0; i < 10; i++) {
this.variable['id'][i] = i*2;
}
this.varExample = true;
}
otherFunction () { // called when i click a button
this.someFunction ();
console.log(this.child.variable); // {}
console.log(this.child.varExample); // true
this.child.initVars();
}
}
Run Code Online (Sandbox Code Playgroud)
父 HTML
<app-child #child [variable]="variable" [varExample]="varExample"></app-child>
Run Code Online (Sandbox Code Playgroud)
子组件
export class ChildComponent {
@Input() variable: any;
@Input() varExample: boolean;
initVars() {
console.log(this.variable); …Run Code Online (Sandbox Code Playgroud)