Methods

You can use the @api annotation to expose a method from a child component to the parent component's controller logic. Component method calls are basically a way to communicate downward through your component's structure, in contrast to events (described in the next section), which can be used to communicate upward.

Methods can return values and/or accept parameters as per normal JavaScript programming semantics. Note that @api only exposes methods to other components in your package and not to components defined by your customers. The following is a basic example:

@api
myMethod(myParameter) {
return 'somevalue';
}

In this next example, the logic in a parent component controller is using the standard browser querySelector method to obtain a reference to a child component. Once an instance of the child component is returned, the code then calls the exposed method directly as usual. The following snippet searches for a child component with the HTML tag name of lightning-datatable and returns the first one found:

var selectedRows = 
this.template.querySelector(
'lightning-datatable').getSelectedRows();
The querySelector method is a browser standard and is also quite advanced in terms of the selector queries it accepts. You might be tempted to try to match on ID; Salesforce does not recommend this, and instead recommends using the HTML tag name, CSS class, or a value assigned to one of the data-* attributes (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*). 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181