3

I have an LWC utility class that imports custom labels and exports, ex.

import customLabelOne from '@salesforce/label/c.customLabelOne';
import customLabelTwo from '@salesforce/label/c.customLabelTwo';

export const customLabels = {
    customLabelOne = customLabelOne,
    customLabelTwo = customLabelTwo
}

This works fine if I import the object into another LWC. However, now I want to use the utility class in Aura components also. So far, I have done this in the .cmp file:

<c:customLabelsUtils aura:id="utils" />

And then in the helper of the Aura, I'm trying to do something like this:

getCustomLabelsUtils : function(component,event,helper){
    var libCmp = component.find('utils');
    const customLabels = libCmp.customLabels;   
    console.log('customLabels type is => ' + typeof(customLabels));
    console.log('customLabels is => ' + customLabels);
    console.log('customLabels.customLabelOne => ' + customLabels.customLabelOne);
},

Which prints out the following:

customLabels type is => function
customLabels is => function [object Object]() { [native code] }
customLabels.customLabelOne => undefined

How do I get the labels in the Aura component?

1 Answer 1

4

That won't work, because the symbol is outside of the default export. Your util component needs to be a full component:

import customLabelOne from '@salesforce/label/c.customLabelOne';
import customLabelTwo from '@salesforce/label/c.customLabelTwo';

const customLabels = {
    customLabelOne,
    customLabelTwo,
}
export { customLabels };

import { LightningElement, api } from 'lwc':
export default class CustomLabelsUtils extends LightningElement {
    @api customLabels = customLabels;
}

Don't forget to include an .html for your utility component.

4
  • I'm mobile right now, I'll update if necessary when I get home.
    – sfdcfox
    Aug 24, 2022 at 21:21
  • Actually that’s not the issue. I didn’t paste the full LWC code, and I mentioned above that the code I have works fine when I import in another LWC. The issue I am facing is specifically what I asked regarding how to get the labels in Aura.
    – Bautista
    Aug 24, 2022 at 23:44
  • @Bautista As I tried to say, Aura can only access public properties, not the other things that you might try to export. You need to follow this pattern if you want your labels available in both Aura and LWC.
    – sfdcfox
    Aug 25, 2022 at 1:53
  • @sfdcfox thanks for this! Works like a charm.
    – Jo_gurt
    Mar 17 at 13:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .