π Decorator
@api : public, μ»΄ν¬λνΈμ μμ±μ΄λ λ©μλλ₯Ό λ ΈμΆ μν΄. @apiλ₯Ό μ¬μ©νκ² λλ©΄ λ€λ₯Έ μ»΄ν¬λνΈμ ν΄λΉ μ»΄ν¬λνΈμ μ κ·Όνκ³ μνΈμμ© κ°λ₯
** λΆλͺ¨ - μμ μ»΄ν¬λνΈμ κ²½μ°, νμ μ»΄ν¬λνΈ(=μμ)μ μ¬μ©
1) λΆλͺ¨μμ μ λ ₯ λ°μ κ°μΌλ‘ μμ μ»΄ν¬λνΈμ css λμ μΌλ‘ λ³λ
childofp2cusingapi
<template>
<lightning-card title="Child with API property" icon-name="utility:user">
<div class="slds-var-m-around_medium">
<p>{percentage}%</p><br/>
<div style={style}>.<br/></div>
</div>
</lightning-card>
</template>
import { LightningElement, api } from 'lwc';
export default class Childofp2cusingapi extends LightningElement {
@api percentage;
get style (){
return `background-color:red; min-height:50px; width: ${this.percentage}%; min-width:10px ; border: 1px`;
}
}
parentofp2cusingapi
html
<template>
<lightning-card title="Parent with API Property" icon-name="utility:user">
<div class="slds-var-m-around_medium">
<lightning-input type="number" value={percentage} onchange={handleonchange}></lightning-input>
<c-childofp2cusingapi percentage={percentage}></c-childofp2cusingapi>
</div>
</lightning-card>
</template>
js
import { LightningElement } from 'lwc';
export default class Parentofp2cusingapi extends LightningElement {
percentage = 20;
handleonchange(event){
this.percentage = event.target.value;
}
}
2) λΆλͺ¨ μ»΄ν¬λνΈμμ λ²νΌμ ν΄λ¦ν΄μ μμ μ»΄ν¬λνΈ μλ‘ κ³ μΉ¨
<template>
<lightning-card title="I am child with API function" icon-name="utility:user">
<div class="slds-var-m-around_medium">
<p>{timestamp}</p>
</div>
</lightning-card>
</template>
import { LightningElement, api } from 'lwc';
export default class Childofp2cusingfunction extends LightningElement {
timestamp = new Date();
@api
refresh() {
this.timestamp = new Date();
}
}
<template>
<lightning-card title="I am Parent with API function" icon-name="utility:user">
<div class="slds-var-m-around_medium">
<lightning-button label="Refresh" onclick={handleClick}></lightning-button>
<br/>
<c-childofp2cusingfunction></c-childofp2cusingfunction>
</div>
</lightning-card>
</template>
import { LightningElement } from 'lwc';
export default class Parentofp2cusingfunction extends LightningElement {
handleClick() {
this.template.querySelector("c-childofp2cusingfunction").refresh();
}
}
3) μμ μ»΄ν¬λνΈμμ λ°μν μ΄λ²€νΈλ‘ λΆλͺ¨ μ»΄ν¬λνΈ κ° λ³κ²½(μ΄λ²€νΈ λ²λΈλ§)
<template>
<lightning-card title="Communicating" icon-name="utility:user">
<div class="slds-var-m-around_medium">
<lightning-button label="Count++" onclick={handleOnclick}></lightning-button>
</div>
</lightning-card>
</template>
import { LightningElement, api } from 'lwc';
export default class ChildComponentCommunication extends LightningElement {
endValue = 5;
handleOnclick() {
// this.dispatchEvent(new CustomEvent('increasecount'));
console.log('HHHIIII');
const myEventWithValue = new CustomEvent('increasecount', {
detail : {
endValue : this.endValue,
msg : "Hello from Semy Leeeeee"
}
});
this.dispatchEvent(myEventWithValue);
}
}
<template>
<lightning-card title="I am Parent with API function" icon-name="utility:user">
<div class="slds-var-m-around_medium">
<lightning-button label="Refresh" onclick={handleClick}></lightning-button>
<br/>
<c-childofp2cusingfunction></c-childofp2cusingfunction>
</div>
</lightning-card>
</template>
import { LightningElement } from 'lwc';
export default class ParentComponentCommunication extends LightningElement {
count = 1;
endValue = 0;
msg = "";
handleEventChange(event) {
this.endValue = event.detail.endValue;
this.msg = event.detail.msg;
if(this.count < this.endValue) {
this.count = this.count + 1;
}
}
}