简体   繁体   English

Aura.js Lightning组件:从上级/上级组件触发嵌套/子组件的方法吗?

[英]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.

相关问题 Aura根组件不处理从其子组件开始的应用程序事件(闪电组件) - Aura root component not handling the application event started from its child (Lightning Component) Vue js,控制子组件从父组件渲染还是将父组件数据的数据同步到子组件数据? - Vue js, controlling the child component renders from parent component or synch the data of parent component's data to child components data? 使用 Charts Js 在 Lightning Web 组件中将数据从父级传递给子级 - Pass data from parent to child in lightning web component using Charts Js React js从父组件更改子组件的state - React js change child component's state from parent component 从 Vue.js 中的子组件更新父组件 - Update parent components from child component in Vue.js 单击子组件而不触发父组件上的clicked事件 - Clicking on Child components without firing clicked event on parent component 如何从父表单组件获取数据到子组件嵌套组件数据从父到子 - How to get data from parent form component to child component nested components data from parents to children 当子组件嵌套了多个父组件时,从子组件调用超级父函数 - Calling to super parent function from child component when it has nested multiple parents 子组件 componentDidMount() 应该调用不同的方法取决于父组件 - Child components componentDidMount() should call different methods depends on parent component 如何使用 vanilla JS Web 组件从子组件引用父组件中的方法? (不是任何框架或库) - How to reference to a method in parent component from child component with vanilla JS Web Components? (Not any framework or Library)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM