LWC - Using a Static Resource

LWC - Using a Static Resource

LWC - Using a Static Resource

Brett M. Nelson - Wednesday, January 29, 2020

Hello, I'm Brett with WIPDeveloper.com. When working with lightning web components, one of the things you might have to access is a static resource. In this case, I would like to display an image inside of one of the Lightning Web Components.

Let's take a look at how to do that.

I've already created a lightning web component to access the static resource. And I've created a static resource two static resources. One has an image and one that a file or folder. I'm going to close those out because we don't need to see them right now, in the template of the component that we're going to access the static resource from, I would like to display the image. So let's create an image element. And our source for the image is actually going to be imagePath, which doesn't exist yet.

Over in our JavaScript class, we don't have an imagePath yet, but let's make one. What we're going to do is use a important statement to get access to the static resource.

Now we have import import imageToUse from '@salesforce/resourceUrl/' and then the name of our static resource, which is imageToUse. Now we'll just assign that to image path. Now if we save this both of these are saved.

I don't have this data, I don't have local development running. So I'm going to start that out. Once this is running, I'll go back to my window. That is the wrong one. I'm going to refresh it here. This is the local development. Good see building stuff. And there we go.

We have an image in our custom lightning web component. Now let's deploy it and make sure it's all working. Okay. Looks like it's done. deploying. There we go. That's accessing a static resource that is a single image.

Now if we want to access an image that's inside a folder, static resource is very similar. We're doing is importing the path of the test files route, static resource. And we're assigning it to folderToUse. And now we need to assign it in our components so that we have access to it. Now we've assigned folderToUse the path default, we've assigned the folderToUse path to folderPath, and we can go back and look at it. That's right there. You see the path relative path to our static resource folder. But if we look in our folder, We see that test files actually contains an image and some JavaScript. And if we can't just put the wipdeveloper-banner.png is we have to create a getter so that we can format the text for it back in the controller, let's create a getter.

So in our getter we are returning to the folder path with wipdeveloper-banner.png appended to it. And now we want to access this in our template will assign it to another assignment as the source to another image tag.

I want to get rid of this folder path right now because we don't need it.

Now we can see that we now have two images on our page. And if we deploy this we see our two images. So it seems pretty straightforward. Once you have access to your static resource, you can access the folder or once you have access to your static resource, you can access the files that it contains, since you have the path for and its relative from the root of the static resource.

One thing to note is when using the local development server, you may encounter issues if you add static resources or modified them after the server has been started. We see right now that the static resources are loading properly locally. But if we change the name of our banner image in the folder and update our controller to point to the new the new file name getting deployed right now, and we'll go back to our component and we see that it's not showing both images. If we look at the console, we'll see that it's missing or can't find that banner image. On the server though, it's it shows two images. So what's going on is the static resources are only copied over to the local development server when it started. So we can cancel out of our local development server and restart it And once it's running again, we can go to our local development time.

And everything's working as expected now. So if you're working with static resources, and using local development and you modified or if you've edited your static resource, you might have to restart the local dev environment.

That鈥檚 it for now.

Remember to sign up for聽 The Weekly Stand-Up!聽 and you can get updated with any new information we have on WIPDeveloper.com.

Tags

  • LWC
  • Salesforce
  • SalesforceDeveloper
  • SalesforceDX

两个鬼故事哥伦比亚地震泸州地震最新消息名典免费起名名字测试网中国式关系36集手机网络电话小天才平板电脑虚拟女友明起姓名2019猪年儿童起名字大全朱姓宝宝起名那金花和她的女婿全集孔荷宠用芮起名字搭什么字好茶楼棋牌室起名部门 起名恶灵骑士在线观看雷武中国有几个直辖市姓马的小孩子起名紫雨风暴煲机软件企业店铺起名淘宝梦见和别人吵架是什么预兆灯具公司怎么起名字宝宝起名大全杨姓太尉恐怖小说下载txt电子书免费下载用诗经楚辞起什么名字好听自助起名网浴池起名少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化