top of page
Writer's pictureNeeraj Singh

How to Utilize a Static Resource in Salesforce LWC


Static Resouce in Salesforce Lwc

Introduction

You can save photos, CSS files, JavaScript files, and more in Salesforce Static Resources, which you can then refer to in your LWC. When you wish to incorporate custom files or external libraries into your component, this is quite helpful.


Step 1: Upload Static Resources

  1. Navigate to Static Resources: Go to Setup > Static Resources.

  2. Upload Your File: Click New and upload your file. Give it a meaningful name, as this will be used to reference the resource in your LWC.

Step 2: Reference Static Resources in LWC

A static resource must be imported into a LWC using the @salesforce/resourceUrl module.

Example:

How to import Static resouce in lwc

Suppose you have uploaded a CSS file named customStyles.css as a static resource.

Step 3: Using Static Resources in HTML

You can also use static resources directly in your HTML template, especially for images

Example:

Html template to store the image syntax in lwc
Static resouce import in javascript of lwc

Step 4: Using JavaScript Libraries

If you're using a JavaScript library, you can load it similarly using the loadScript method.

Example:

How to use Javascript Library in lwc for static resource

Conclusion

Using static resources in LWC allows you to enhance your components with external assets like CSS, JavaScript libraries, and images. This guide walks you through the steps to upload and reference these resources in your LWC components, enabling you to create more dynamic and visually appealing Salesforce applications.

142 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page