How to use custom labels in lwc salesforce.

  • Post author:
  • Post category:Blog
  • Post comments:0 Comments

Custom Labels are very useful to save your time and help us manage code redundancy . You can refer custom label in VF page and lighting component as well , how ? we will give you can simple and easy way to use custom labels in you lwc component.

Refer this link to see how to make use of custom label into VF page.

You can create a custom label from your local VS code project or from your salesforce org. here I have this CustomLabels.labels-meta.xml file which contains my custom labels.

CustomLabels.labels-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">
    <labels>
        <fullName>Demo_Label_1</fullName>
        <language>en_US</language>
        <protected>true</protected>
        <shortDescription>Test Label</shortDescription>
        <value>I am Demo custom Label !</value>
    </labels>
</CustomLabels>

Once you create a custom label you have to import that into your component.

Customlabel.js

import { LightningElement,track } from 'lwc';
import Demo_Label_1 from '@salesforce/label/c.Demo_Label_1';

export default class CustomLabel extends LightningElement {
    @track customLabelValue = Demo_Label_1;
}

Here I have imported my Demo_Label_1 and stored that into customLabelValue this local variable .Now we can refer this customLabelValue variable anywhere in our component .

CustomLabel.html

<template>
    <div class="slds-card">
        <h1>{customLabelValue}</h1>
    </div>
</template>

This is how you can use custom labels into your lwc component.

Leave a Reply