我有两个子组件的应用程序组件,即第一个和第二个,我正在通过路由器出口标签导航。单击子组件按钮时,我想将父组件变量更改为“XYZ”。
app.component.html
<a routerLink = "/first">
First
</a>
<a routerLink = "/second">
Second
</a>
<div>
<router-outlet></router-outlet>
</div>
{{myvar}}
app.component.ts
export class AppComponent {
title = 'childParentInteraction';
myvar : String = "ABCD";
}
应用程序路由.module.ts
const routes: Routes = [
{
path:"first", component: FirstComponent
},
{
path:"second",component: SecondComponent
}
];
first.component.html
<div>
<p>first works!</p>
<button (click)="changeVar()">
Change variable
</button>
</div>
first.component.ts
export class FirstComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
changeVar(){
}
}