[英]Aura.js Lightning Components: Firing a nested/child component's methods from the super/parent component?
I'm having issues communicating to a nested component. 我在与嵌套组件通信时遇到问题。 I would like a component to be able to run a method in the nested-component's controller.js (or helper). 我希望组件能够在嵌套组件的controller.js(或帮助器)中运行方法。 I have tried both events and via and no luck. 我已经尝试了两个事件和通过,但没有运气。 Here is the example markup: 这是示例标记:
<!-- myEvent.evt -->
<aura:event type="APPLICATION">
</aura:event>
<!-- super-component -->
<aura:component>
<aura:registerEvent name="myEventName" type="c:myEvent"/>
<c:my_Nested_Component />
<ui:button press="{!fireEvent}"
<aura:component>
//super-component_controller.js
({
fireEvent: function(component){
var myEvent = component.getEvent("myEventName");
myEvent.fire();
console.log('event fired');
}
})
------------------------------------------
<!-- nested-component -->
<aura:component>
<aura:handler name="myEventName" event="c:c:myEvent" action="{!c.gotEvent}" />
<aura:component>
//nested-component_controller.js
({
gotEvent: function(component, event){
console.log('received event!');
}
})
This does not work. 这是行不通的。 I tried the same exact code that I placed on the nested-component on a super-super-component, and it worked perfectly.The super-super component received the event. 我尝试了与放置在super-super-super组件上的嵌套组件上的代码完全相同的代码,并且效果很好.super-super-super组件收到了事件。 But the nested component is not able to. 但是嵌套组件不能。 I figured this had to do with events only bubbling up (though the documentation does say that that is only the case with Component events, not application events). 我认为这只与冒泡的事件有关(尽管文档确实说,只有组件事件才是这种情况,而应用程序事件则没有)。
So the other option I read online is using . 因此,我在线阅读的另一种选择是使用。 I tried doing this, but this too did not work for speaking to a nested component. 我尝试这样做,但是这对于与嵌套组件进行通信也不起作用。
How can a parent component cause a method on the nested component to fire? 父组件如何导致嵌套组件上的方法触发?
Thank you 谢谢
This issue was answered here: https://salesforce.stackexchange.com/questions/108544/lightning-components-firing-a-nested-child-components-methods-from-the-super-p 这个问题在这里得到了回答: https : //salesforce.stackexchange.com/questions/108544/lightning-components-firing-a-nested-child-components-methods-from-the-super-p
The problem was that I was using component.getEvent() instead of $A.get(), which is required for Application level events. 问题是我使用的是component.getEvent()而不是$ A.get(),这是应用程序级事件所必需的。 Additionally, the name of the registered event is not the way to reference the event from the handler, but rather using it's actual file name, like so: 此外,注册事件的名称不是从处理程序引用事件的方式,而是使用其实际文件名,如下所示:
//super-component_controller.js
({
fireEvent: function(component){
var myEvent = $A.get("e.c:myEvent");
myEvent.fire();
console.log('event fired');
}
})
<!-- nested-component -->
<aura:component>
<aura:handler event="c:myEvent" action="{!c.gotEvent}" />
<aura:component>
Thank you to @MohithShrivastava for figuring this one out! 感谢@MohithShrivastava解决这一问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.