简体   繁体   English

如何从父组件访问作为子路由的组件?

[英]How can I get access to a component that is a child route from the parent component?

I have something like this in my routes.我的路线中有这样的事情。

const routes: Routes = [
  { 
    path: 'master', 
    component: MasterComponent, 
    children: [
      {
        path: 'first',
        component: FirstComponent
      },
      {
        path: 'second',
        component: SecondComponent
      }
    ]
  }
];

The idea is that when I go to the address http://site/master/*option* it will render the master component plus whichever component I am putting in *option* .这个想法是,当我转到地址http://site/master/*option* ,它将呈现主组件加上我放入*option*任何组件。

The master component has a <router-outlet> inside of it, and in there it will render the "option" I'm navigating to.主组件内部有一个<router-outlet> ,它会在其中呈现我要导航到的“选项”。 That is working perfectly, and rendering just fine.这工作完美,渲染也很好。 However, what I want to do now is from Master find a value inside the option component I navigated to.但是,我现在要做的是从 Master 中找到我导航到的选项组件中的值。 An example is a title I will display in Master which varies depending on the "option" component I'm navigating to.一个例子是我将在 Master 中显示的标题,它根据我导航到的“选项”组件而有所不同。 Is there any way I can achieve this?有什么办法可以实现这一目标吗? Or maybe there's a better way to do it.或者也许有更好的方法来做到这一点。

I was trying to use ViewChild or ViewChildren but both require that you define the type of the Component, and since the component will not always be the same, it's not viable.我试图使用ViewChildViewChildren但两者都要求您定义组件的类型,并且由于组件并不总是相同的,因此它不可行。

You can achieve this with 2 method您可以使用 2 种方法实现此目的

Method #1方法#1

Use activate event of router outlet使用路由器插座的activate事件

<!-- master.component.html -->
<router-outlet (activate)="onActivate($event)"></router-outlet>
//master.component.ts
onActivate(componentRef){
  console.log(componentRef.title); //access methods and properties of current loaded component
}
Method #2方法#2

Add title variable in service and change it in child component在服务中添加标题变量并在子组件中更改它

//master.service.ts
title = '';

changeTitle(title) {
 this.title = title;
}
//first.component.ts
ngOnInit() {
  this.masterService.changeTitle('First');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从父路由的组件访问已激活的子路由的数据? - How can I access an activated child route's data from the parent route's component? 如何从 Angular 中的子路由组件访问父组件属性? - How to access parent component properties from a child route component in Angular? 如何使用activatedRoute从子组件获取父路由参数? - How can I get a parent route params from a child component using activatedRoute? 如何从子组件中访问映射到父组件的路由参数? (角度 2.0) - How do I access route parameters that are mapped to a parent component from within a child component? (angular 2.0) 如何将数据从子组件传递到父组件? - How can I pass data from child component to parent component? 当我使用路由器插座时,如何从父级制作子级路由中的空组件 - How can i make empty component in the child route,from the parent,when i use router outlet 如何从角度2的子组件中的父组件获取变量? - how can i get variable from parent Component in the child component in angular 2? 如何在Angular中从父组件到子组件获取数据? - How can I get data from parent component to child component in Angular? 如何从父组件访问子routeParams? - How do I access child routeParams from a parent component? 无法从父组件访问子组件功能 - Can't able to access child component function from parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM