Apex Class :
meta.xml:
JS :
HTML:
Apex Class :
meta.xml:
JS :
HTML:
A Decorator is a design pattern that allows adding behaviors to Javascript Objects. Decorators which are part of ECMAScript are used to dynamically alter or modify the functionality.
There are three types of Decorators in Lightning web components.
Let's see them in detail.
To expose a public property or a public method, decorate with @api. Public properties are reactive, also known as public reactive properties since if the value of property changes then the component is re-rendered
Please find the below code snippet that provides an insight into how an @api property on a child component is set from a parent component:
apiDecoratorSampleChildComponent.html
<template>
<lightning-card title="Child Component">
<div class="slds-p-around_medium">
<p class="slds-p-horizontal_small">{message}</p>
</div>
</lightning-card>
</template>
apiDecoratorSampleChildComponent.js
import { LightningElement, api } from 'lwc';
export default class ApiDecoratorSampleChildComponent extends LightningElement {
@api message;
}
apiDecoratorSampleChildComponent.js-meta.xml
apiDecoratorSampleParentComponent.html
<template>
<c-api-decorator-sample-child-component message = 'Message From Parent Component!!'></c-api-decorator-sample-child-component>
</template>
apiDecoratorSampleParentComponent.js
import { LightningElement } from 'lwc';
export default class ApiDecoratorSampleParentComponent extends LightningElement {}
To expose private property or a private method, declare with @track. Also known as Private reactive properties
helloWorld.html
<template>
<lightning-card title="Hello World" icon-name="custom:custom14">
<div class="slds-m-around_medium">
<p>Hello, {greetingMessage}!</p>
<lightning-input label="Name" value={greeting} onchange={changeHandler}></lightning-input>
</div>
</lightning-card>
</template>
helloWorld.js
import { LightningElement } from 'lwc';
export default class HelloWorld extends LightningElement {
//@track greetingMessage = 'World';//Before Spring ’20 to need to import track decorator & use it to make a field reactive
greetingMessage = 'World';
changeHandler(event) {
this.greetingMessage = event.target.value;
}
}
Syntax:
import <methodName> from ‘@salesforce/apex/<Namespace.ApexClassName.apexMethod>’;
@wire(methodName, {methodParams})
propertyOrFunction;
methodName: A variable that identifies the Apex method.
apexMethod: The name of the Apex method to import.
ApexClassName: The name of the Apex class.
Namespace: Defines the namespace of the Salesforce organization. Only specify a namespace when the organization doesn’t use the default namespace (c)
displayContacts.html
<template>
<lightning-card title="Contacts" icon-name="standard:contact_list">
<div class="slds-m-around_medium">
<template if:true={contacts.data}>
<template for:each={contacts.data} for:item="con">
<p key={con.Id}>{con.Name}</p>
</template>
</template>
</div>
</lightning-card>
</template>
displayContacts.js
import { LightningElement, wire } from 'lwc';
import getContactsList from '@salesforce/apex/ContactsService.getContacts';
export default class DisplayContacts extends LightningElement {
@wire(getContactsList) //Wiring the Output of Apex method to a property
contacts;
}