简体   繁体   English

路由到angular4中的父组件或从父组件路由

[英]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, 所以这是解决方案,

  • Assume `homeComponent` is the component which loads when app starts. 假设“ homeComponent”是在应用启动时加载的组件。
  • It has a button to go to about page(string coming from HTML & using typescript code) 它有一个转到页面的按钮(字符串来自HTML并使用打字稿代码)
  • It has a router.navigate method which takes an appropriate route name and navigate it accordingly. 它具有router.navigate方法,该方法采用适当的路由名称并进行相应的导航。
  • And Vice versa 反之亦然

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.

相关问题 Angular4-从父组件到子模式组件的方法调用 - Angular4 - Call a method from parent component to child modal component 将数据从子组件传递到父组件Angular4 - pass data from child component to parent component Angular4 如何在Angular4中将数据从子组件传递到父组件 - How to pass data from child component to parent component in Angular4 Angular4 @Input()将值从父组件传递到子组件 - Angular4 @Input() to pass value from parent component to child component 无法在angular4中将数据从父组件传递到子组件 - Not able to pass data from parent to child component in angular4 父组件中的触发事件来自路由Angular中子组件的加载 - Trigger event in Parent component from child component load in routing Angular 在angular4中使用锚标记“ href”从子组件查看父组件中内容的内容? - View of content in parent component from child component using anchor tag 'href' in angular4? Angular4如何在确保父对象组件变量未定义之后从父组件变量中为其分配变量 - Angular4 how to assign variable in child component from parent component variable after reassuring that it is not undefined 如何验证子组件 NgForms 与 Angular4 中的父组件有 3 个不同的 NgForm - How to validate the child component NgForms having 3 different NgForm from the parent component in Angular4 如何通过更改Angular4中子组件中的值来更新父组件中数组的长度? - How can I update length of array in parent component from changing value in child component in Angular4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM