EP.4 Communicate With Events in LWC

호둀둀·2023λ…„ 7μ›” 26일
0

λ½€μŠ€λͺ¨

λͺ©λ‘ 보기
3/11

πŸ”— Decorator
@api : public, μ»΄ν¬λ„ŒνŠΈμ˜ μ†μ„±μ΄λ‚˜ λ©”μ„œλ“œλ₯Ό λ…ΈμΆœ μ‹œν‚΄. @apiλ₯Ό μ‚¬μš©ν•˜κ²Œ 되면 λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ— ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈμ— μ ‘κ·Όν•˜κ³  μƒν˜Έμž‘μš© κ°€λŠ₯
** λΆ€λͺ¨ - μžμ‹ μ»΄ν¬λ„ŒνŠΈμ˜ 경우, ν•˜μœ„ μ»΄ν¬λ„ŒνŠΈ(=μžμ‹)에 μ‚¬μš©


πŸͺ“ 예제


1) λΆ€λͺ¨μ—μ„œ μž…λ ₯ 받은 κ°’μœΌλ‘œ μžμ‹ μ»΄ν¬λ„ŒνŠΈμ˜ css λ™μ μœΌλ‘œ 변동

  • childofp2cusingapi

    • html
      <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>
    • js
      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) λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œ λ²„νŠΌμ„ ν΄λ¦­ν•΄μ„œ μžμ‹ μ»΄ν¬λ„ŒνŠΈ μƒˆλ‘œ κ³ μΉ¨

  • childofp2cusingfunction
    • html
      <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>
    • js
      import { LightningElement, api } from 'lwc';
      export default class Childofp2cusingfunction extends LightningElement {
        timestamp = new Date();
        @api
        refresh() {
            this.timestamp = new Date();
        }
      }
  • parentofp2cusingfunction
    • html
      <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>
    • js
      import { LightningElement } from 'lwc';
      export default class Parentofp2cusingfunction extends LightningElement {
        handleClick() {
            this.template.querySelector("c-childofp2cusingfunction").refresh();
        }
      }

3) μžμ‹ μ»΄ν¬λ„ŒνŠΈμ—μ„œ λ°œμƒν•œ 이벀트둜 λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈ κ°’ λ³€κ²½(이벀트 버블링)

  • childComponentCommunication
    • html
      <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>
    • js
      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);
        }
      }
  • parentofp2cusingfunction
    • html
    <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>
    • js
    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;
            }
        }
    }

0개의 λŒ“κΈ€