EP.6 Wire Adapters & Functions

ํ˜ธ๋กค๋กคยท2023๋…„ 7์›” 26์ผ
0

๋ฝ€์Šค๋ชจ

๋ชฉ๋ก ๋ณด๊ธฐ
5/11

๐Ÿ”— Wire Adapter
์ฐธ๊ณ (Salesforce Developer)

๐Ÿช“ ์˜ˆ์ œ


โ†’ ์ขŒ์ธก ๋นˆ ๋ถ€๋ถ„์— ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ฝ์ž…๋˜์–ด์žˆ์Œ. js์—์„œ @wire๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ™”๋ฉด์— ๋“ค์–ด๊ฐ€์ž๋งˆ์ž function์ด ์‹คํ–‰๋จ.

1) wireWithProperty

  • html
<template>
    <P>
        <b>Account Name : {name} </b>
    </P>
</template>
  • js
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name';

export default class WireWithProperty extends LightningElement {
    @api recordId; // ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” ๋ ˆ์ฝ”๋“œ ํŽ˜์ด์ง€์˜ ๋ ˆ์ฝ”๋“œ ID๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ(Parent-Child ๊ด€๊ณ„์—์„œ api๋Š” Child์—์„œ ์‚ฌ์šฉ ํ•ด์•ผํ•จ)
    @wire(getRecord, {recordId : '$recordId', fields : [ACCOUNT_NAME_FIELD]})
    record;
    get name() {
        return getFieldValue(this.record.data, ACCOUNT_NAME_FIELD);
    }
     
}

2) wireWithFunciton : function์„ ์‚ฌ์šฉํ•ด์„œ ์—๋Ÿฌ์— ๋Œ€ํ•œ ์ œ์–ด ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๊ณ  ์˜จ ํ›„ ์ˆ˜ํ–‰ํ•  ๋กœ์ง์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ

  • html x
  • js
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

export default class WireWithFunction extends LightningElement {
    @api recordId;
    accounts; // @wire๋กœ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
    error; // error ์ €์žฅ

    @wire(getRecord, { recordId : '$recordId', fields : ['Account.Name']})
    wireAccount({ error, data }) {
        if(data) {
            console.log('### Getting data from wire with function');
            this.accounts = data;
        } else if(error) {
            this.error = error;
            console.log('### error : ' + error.body.message);
        }
    }
}

0๊ฐœ์˜ ๋Œ“๊ธ€