[英]routing to and from the parent component in angular4
Senario: I have 2 component in my app. Senario:我的应用程序中有2个组件。 I want to load the first component on page load and the component will have a link to navigate to the second component.
我想在页面加载时加载第一个组件,该组件将具有一个导航到第二个组件的链接。 The second component would in turn have a link to navigate back to the first component.
第二个组件又将具有一个链接,以导航回第一个组件。
I am able to route with 2 routerLink above the router-outlet and move to and fro using the link. 我可以使用路由器出口上方的2 routerLink进行路由,并使用该链接来回移动。 But how can I achieve the above scenario?
但是如何实现上述方案? Any basic plunker example would help.
任何基本的例子都是有帮助的。
I believe you have achieved everything. 我相信您已经取得了一切。 You only need to know how to achieve the same without using
routerLink
link. 您只需要知道如何在不使用
routerLink
链接的情况下实现相同的routerLink
。 So here is the solution, 所以这是解决方案,
home.html home.html
<button (click)="goto('about')"> go to about page </button>
home.ts home.ts
import {Router} from '@angular/router'
export class HomeComponent{
consturctor(private router:Router){}
goto(navigateTo:string){
this.router.navigate([navigateTo]) //this line will take you to about component
}
}
about.html about.html
<button (click)="goto('home')"> go to about page </button>
about.ts 关于
import {Router} from '@angular/router'
export class AboutComponent{
consturctor(private router:Router){}
goto(navigateTo:string){
this.router.navigate([navigateTo]) //this line will take you to home component
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.