如何从父组件调用路由器出口子组件方法 [英] How to call router outlet child component method from parent comonent
本文介绍了如何从父组件调用路由器出口子组件方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是angular2的新手
I am new to angular2
<parent>
<router-outlet><router-outlet>
</parent>
父组件中有一个按钮,如果单击该按钮,它将在子组件(已在路由器出口中加载)中调用方法.
I have a button in parent component, if I click that button it should call a method in the child component (which is loaded in router outlet.)
有什么方法可以从父级调用子级组件(在路由器出口)吗?
Is there any way to call the child component (in router outlet) method from parent ?
推荐答案
将子级放在router-outlet
中,您可以使用ContentChild
来调用子级中的方法.所以...
With child in router-outlet
, you can use ContentChild
to be able to call a method in the child. So...
import { ContentChild } from '@angular/core';
在您的父母中:
@ContentChild(ChildComponent)
private childComponent: ChildComponent;
并在您的点击事件上执行:
and on your click event do:
this.childComponent.doSomething()
此外,您还需要在父级的providers数组中添加子组件:
Also you need to add your child component in the providers array in parent:
@Component({
selector: 'parent',
...
providers: [ChildComponent]
})
这篇关于如何从父组件调用路由器出口子组件方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文