An Aura.Action is a reference to an action in the framework. If a child component has an Aura.Action attribute, a parent component can pass in an action handler when it instantiates the child component in its markup. This pattern is a shortcut to pass a controller action from a parent component to a child component that it contains, and is used for on* handlers, such as onclick.
Example
This example demonstrates how to pass an action handler from a parent component to a child component.
Here’s the child component with the Aura.Action attribute. The onclick handler for the button uses the value of the onclick attribute, which has type of Aura.Action.
<!-- child.cmp -->
<aura:component>
<aura:attribute name="onclick" type="Aura.Action"/>
<p>Child component with Aura.Action attribute</p>
<lightning:button label="Execute the passed action" onclick="{!v.onclick}"/>
</aura:component>
Here’s the parent component that contains the child component in its markup.
<!-- parent.cmp -->
<aura:component>
<p>Parent component passes handler action to c:child</p>
<c:child onclick="{!c.parentAction}"/>
</aura:component>
When you click the button in c:child, the parentAction action in the controller of c:parent is executed.
Instead of an Aura.Action attribute, you could use <aura:registerEvent> to register an onclick event in the child component. You’d have to define the event and create an action in the child’s controller to fire the event. This event-based approach requires a few extra steps but it’s more in line with standard practices for communicating between components.
No comments:
Post a Comment