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

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

例如以下我的一段代码:

<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/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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值