Monday, 27 July 2020

Share Custom labels between Lightning Web Components

We know how to use custom label in lightning web components. What about if you have large list of labels and you want to to use in multiple LWC components? In this post we will talk about best practice to import bulk custom label in LWC and how to create utility class to import same set of label in multiple Lightning web components.

You can create a utility component that imports all the labels and then exposes them bundled together in one object via a method. Then your main component will have a cleaner list of imports.

Step 1) Create Utility Class.


Create a Lightning web component that contains only a single Js file. Like below structure

labelUtility
   ├──labelUtility.js
   └──labelUtility.js-meta.xml

Lets create one common label Utility component.

labelUtility.js
import header from '@salesforce/label/c.Header';
import title from '@salesforce/label/c.title';

const label = {
    header: header,
    title: title
};

export {label};
  • The module should only have a JS file & Metadata file in it, otherwise it will not work.
  • Just like with any Lightning web component, the folder name and the filename must be identical
  • Imports all the labels and then exposes them bundled together with export.

Step 2) How to use shared label Utility


Create new Lightning web component and import Js file with import statement like below

import { label  } from 'c/labelUtility';

Let see the full code.

import { LightningElement,track } from 'lwc';
import { label  } from 'c/labelUtility';
export default class CustomLabelDemo extends LightningElement {
    @track myLabel=label;
}     
  • Import JS file with import

<template>
    <lightning-card  title={myLabel.title} variant="narrow" icon-name="standard:opportunity">
        <p>{myLabel.header}</p>
    </lightning-card>
</template>   


Please share your feedback and comment if there is any better way to implement or your like this post.





25 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Very useful points for beginners. I'm glad you shared it.

    ReplyDelete
  3. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    ReplyDelete
  4. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! Custom food labels

    ReplyDelete
  5. Thanks for sharing this valuable knowledge regarding the use of the custom labels in lightning web components. I am a beginner in the Salesforce profession and these steps will definitely help me gain more knowledge about Salesforce. Keep posting this type of knowledgeable content.

    ReplyDelete
  6. This blog is so cool. I am learning salesforce course online. This blogs helps me find out better understanding about salesforce training. Thank you for this blog!

    ReplyDelete
  7. onnect with people who matter the most for your business with our Community cloud Implementation service or providers.

    ReplyDelete
  8. Brother, your content is awesome and very informative. I also have a website , please visit and share you review too
    Brahmrishi is very good website
    jaisana

    ReplyDelete
  9. I really like and appreciate your post.Really thank you! Fantastic.
    salesforce training
    salesforce online training

    ReplyDelete
  10. Thanks for sharing this information. Your blog is good and I have bookmarked it as well.


    SevaHome is the online site to buy scented candles and to buy premium candles in India. We have christmas collection, metal collection , Glass collection and marble collection. You can Seva Gift Sets from our website as well.

    ReplyDelete
  11. Insomnia is a sleep condition in which you consider it impossible to fall asleep. .buy Ambien Sanval 10mg online It may be short-term or long-term (acute) or chronic. Sleeplessness is chronic if it lasts three nights or longer per week, at least. Decide to for benefits

    ReplyDelete
  12. Anxiety is a behavioral aspect present in all human beings. Buy Clonazepam(Klonopin) 2mg Online The only fact that can create some difference is that when this is beyond one’s control, it comes to a disorder which might end up to panic attacks and even severe seizure attacks. This continuous existence of anxiety in an individual might end up causing depression, distress, and nervousness.

    ReplyDelete
  13. Are you suffering from unbearable muscle and bone pain? Has your doctor or pharmacist prescribed you SOMA for relief from the discomfort and pain associated with the painful, Buy soma 350 mg online acute musculoskeletal condition

    ReplyDelete

  14. If that is what one’s life is, 
    Tramadol 50 mg order online is the answer to your prayers. As effective as it is in relieving pain, it does come with some side effects, so stay alert to symptoms of dizziness, drowsiness, blurred vision, sleep problems, agitation, anxiety, constipation, diarrhea, hallucination, nausea, tremor, vomiting, and diaphoresis.

    ReplyDelete
  15. Modafinil plays a part in calming the brain of the patient. Order Modafinil 200 mg in the USA on Lowest Price The exact mode of its function is still unestablished. It improves wakefulness by amplifying the dopamine amount, which turned out to be a neuro-emitter material that tricks nerves to stay up-to-date inside the brain by curtailing the nerves ‘ supplies of dopamine. and begin your treatment.

    ReplyDelete
  16. Male sexual problems such as impotence or erectile dysfunction has gained prominence all over the world. Buy levitra 10 mg online There are innumerable men who complain about having a problem of impotence. Due to this problem, they are unable to perform sexual activities

    ReplyDelete