Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
$Resource
Using $Resource lets you reference assets by name, without worrying about the gory details of URLs or file paths. You can use $Resource in Aura component markup and within JavaScript controller and helper code.
Using $Resource in Component Markup
1<aura:component>
2 <!-- Stand-alone static resources -->
3 <img src="{!$Resource.generic_profile_svg}"/>
4 <img src="{!$Resource.yourNamespace__generic_profile_svg}"/>
5
6 <!-- Asset from an archive static resource -->
7 <img src="{!$Resource.yourGraphics + '/images/logo.jpg'}"/>
8 <img src="{!$Resource.yourNamespace__yourGraphics + '/images/logo.jpg'}"/>
9</aura:component>1<aura:component>
2 <ltng:require
3 styles="{!$Resource.jsLibraries + '/styles/jsMyStyles.css'}"
4 scripts="{!$Resource.jsLibraries + '/jsLibOne.js'}"
5 afterScriptsLoaded="{!c.scriptsLoaded}" />
6</aura:component>Using $Resource in JavaScript
To obtain a reference to a static resource in JavaScript code, use $A.get('$Resource.resourceName').
1({
2 profileUrl: function(component) {
3 var profUrl = $A.get('$Resource.yourGraphics') + '/images/avatar1.jpg';
4 alert("Profile URL: " + profUrl);
5 }
6})$Resource Considerations
Global value providers in the Aura Components programming model are, behind the scenes, implemented quite differently from global variables in Salesforce. Although $Resource looks like the global variable with the same name available in Visualforce, formula fields, and elsewhere, there are important differences. Don’t use other documentation as a guideline for its use or behavior.
Here are two specific things to keep in mind about $Resource in the Aura Components programming model.
First, $Resource isn’t available until the Aura Components programming model is loaded on the client. Some very simple components that are composed of only markup can be rendered server-side, where $Resource isn’t available. To avoid this, when you create a new app, stub out a client-side controller to force components to be rendered on the client.
Second, if you’ve worked with the $Resource global variable, in Visualforce or elsewhere, you’ve also used the URLFOR() formula function to construct complete URLs to specific resources. There’s nothing similar to URLFOR() in the Aura Components programming model. Instead, use simple string concatenation, as illustrated in the preceding examples.