wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Global value providers in LWC


Drinking the new CoolAid one has to come to terms with the old ways. We had a first glimpse before.

Same but different, reloaded

When developing lightning components using the Aura Framework you could use a series of global value providers that give you access to various data sets: $ContentAsset, $Labels, $Locale, and $Resource.

While this convenient, it pollutes the global name space and it a very proprietary (albeit popular at its time) way to provide information. LWC fixes this in a very standard compliant way. This became possible thanks to the new capabilities in the JavaScript ES6 standard.

In LWC all information provided by Salesforce gets added using ES6 import statements from the @salesforce name space. While that syntax is new to Salesforce developers, it is old news for the rest of JavaScript land. So here you go:

  • $ContentAsset -> import assetName from @salesforce/contentAssetUrl/[AssetName]
  • $Labels -> import labelName from @salesforce/label/[LabelName]
  • $Locale -> import i18nproperty from @salesforce/i18n/[internationalizationProperty] (with various values)
  • $Resource -> import resourceName from @salesforce/resourceUrl/[resourceName]
  • current User Id -> import userId from @salesforce/User/Id

The @salesforce name space provides access to additional data, like apex and schema.

As usual YMMV


Posted by on 01 March 2019 | Comments (1) | categories: Lightning Salesforce WebComponents

Comments

  1. posted by SC on Friday 07 February 2020 AD:

    Just want to say that the value being imported is not necessarily the "name". As the scoped module title suggests, it is typically something else. For example, a more accurate import would be:

    import assetUrl from @salesforce/contentAssetUrl/[AssetName]