【小程序】本地图片未加载成功的情况 Failed to load local image resource /pages/XXX/

本文探讨了在小程序开发中遇到的图片加载路径错误问题。当图片路径不在项目相关路径中时,会导致404错误。通过使用wx:if条件渲染,确保只有在photo变量有值时才加载图片,从而解决了后台报错的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发小程序的时候,发现在加载图片时并没有异常,但是后台却报错了。

例如以下我的一段代码:

<image bindtap="chooseImg" class='photo' src='{{photo}}'></image>

这里的{{photo}}是JS传进来的一个变量,但是在显示某个框框的时候将photo的值设为一个路径值,即在选择图片的时候,会因为其路径并不是在该项目的相关路径中,导致报以下错误:

 

Failed to load local image resource /pages/xxx/ 
 the server responded with a status of 404 (HTTP/1.1 404 Not Found) 

那此时的路径{{photo}}相当于是一个无效的路径,在渲染页面的时候,photo还没有被赋值,这就对该路径添加wx:if来表示加载与否。加上wx:if='{{photo}}'

<image wx:if='{{photo}}' bindtap="chooseImg" class='photo' src='{{photo}}'></image>

然后后台就不会再报错啦(*^▽^*)

[渲染层网络层错误] Failed to load local image resource /pages/identity/job/detail/photos/1754886199866_tFRQHlmOipkm45689767cf455deade26d83a8c0f9853.png the server responded with a status of 500 (HTTP/1.1 500 Internal Server Error) (env: Windows,mp,1.06.2504010; lib: 3.9.0)Request URL: http://127.0.0.1:53914/__pageframe__/profile/upload/2025/08/11/4_20250811145609A019.jpg Request Method: GET Status Code: 500 Internal Server Error Remote Address: 127.0.0.1:53914 Referrer Policy: strict-origin-when-cross-origin Connection: Keep-Alive Date: Mon, 11 Aug 2025 07:20:37 GMT PROXY_ERROR: Error%3A%20ENOENT%3A%20no%20such%20file%20or%20directory%2C%20open%20'D%3A%2F%E6%A1%8C%E9%9D%A2%E6%96%87%E4%BB%B6%2Fminni%20-%20%E5%89%AF%E6%9C%AC%20(5)%2Fprofile%2Fupload%2F2025%2F08%2F11%2F4_20250811145609A019.jpg' Transfer-Encoding: chunked Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9 Connection: keep-alive Host: 127.0.0.1:53914 Referer: https://servicewechat.com/wx121a46991ab164fd/devtools/page-frame.html Sec-Fetch-Dest: image Sec-Fetch-Mode: no-cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1 wechatdevtools/1.06.2504010 MicroMessenger/8.0.5 Language/zh_CN webview/56 desktopapp miniprogram port/12491 token/c5ec4ec0fcb2f6b62a5d21a9c4b7e234 runtime/2 sessionid/8
最新发布
08-12
根据提供的信息,报错信息"Failed to load local image resource /pages/publish/h the server responded with a status of 500"是在使用uni-app开发微信小程序时遇到的问题。根据引用中的描述,这个问题可能是由于在微信开发者工具中编译加载图片图片路径为undefined导致的。 根据引用的内容,可以使用计算属性Compute()来解决初次加载图片为空的问题。需要在页面的HTML代码中添加相应的代码,并在Computed()中定义一个计算属性来返回正确的图片路径。 在你的代码中,可以尝试使用以下方式来解决这个问题: ``` <view class="topbox" :style="{backgroundImage: bgImage}"></view> ``` 同时,在Computed()中添加以下代码: ``` computed: { bgImage() { return 'url(' + require('@/static/icon/register/bg03.png') + ')'; } }, ``` 这样,计算属性bgImage会返回正确的图片路径,解决了图片加载为空的问题。 希望这个解决方案能够帮助你解决问题。如果问题仍然存在,请提供更多的上下文信息以便我们更好地帮助你。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [ Failed to load local image resource undefined the server responded with a status of 500](https://blog.youkuaiyun.com/m0_46570767/article/details/121116741)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Optimizing the Quality of Service for a Pub/Sub System](https://download.youkuaiyun.com/download/weixin_38526612/15114801)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值