uniapp、Vue中 image 如何设置默认图片,图片地址加载失败(404)的话就显示默认图片

一、解决方法(在单层循环中)
在这里插入图片描述

HTML:
在image标签当中添加@error,用来监听图片地址加载失败,同时传递参数

<image v-else :src="item" mode="aspectFit" @error="imgerror($event, index)"></image>

JS:

imgerror(e, index) {  
	this.xiangqingUrl.splice(index, 1)
	this.xiangqingUrl.splice(index, 0, '/static/img/noimg.png')
},

单层结构默认图片可能因为数组长度没有发生改变,因此Vue不会更新数据,因此我们需要删除一个元素后再新增一个默认图片地址,使得Vue发生数据更新。

二、双层循环嵌套解决方法
在这里插入图片描述

HTML:
在image标签当中添加@error,用来监听图片地址加载失败,同时传递参数

<swiper-item v-for="(value , indexs) in item.phonts" :key="indexs" >
	<video v-if="value.indexOf('mp4') >-1" :src="value" controls></video>
	<image v-else :src="value" mode="aspectFit" @error="imgerror($event, indexs,index)"></image>	
</swiper-item>

JS:

imgerror(e, indexs,index) {
	var ps = JSON.parse(JSON.stringify(this.list[index].phonts))
	this.$set(this.list[index],this.list[index].phonts,[])
	ps[indexs] = '/static/img/noimg.png'
	this.list[index].phonts = ps
},

解释:因为vue的数据绑定存在一些问题,所以在双层遍历当中会出现数据更新问题,因此我们需要给图片数组重新更换地址
注:如果图片长度不改变,vue默认不会更新,所以我们要把数组赋值为空,然后重新赋值(默认图片地址)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 当图片渲染404时,Vue提供了一种解决方案,即使用`v-bind`指令绑定一个`onerror`事件,当图片加载失败时,触发`onerror`事件,我们可以在该事件指定一个默认图片地址。具体实现如下: ```html <template> <img v-bind:src="imageUrl" v-bind:onerror="setDefaultImage" /> </template> <script> export default { data() { return { imageUrl: "http://example.com/image.jpg", defaultImageUrl: "http://example.com/default.jpg", }; }, methods: { setDefaultImage(event) { event.target.src = this.defaultImageUrl; }, }, }; </script> ``` 在上面的代码,我们在`<img>`标签上使用了`v-bind`指令绑定了`src`属性和`onerror`事件。当图片加载失败时,会触发`onerror`事件,并执行`setDefaultImage`方法。`setDefaultImage`方法,我们将图片的`src`属性修改为默认图片地址,从而实现了默认图片显示。 ### 回答2: 在Vue,当图片渲染404时,可以通过使用v-bind指令和v-on指令来添加默认图片。具体步骤如下: 1. 在Vue组件,为需要渲染图片的img标签添加v-bind指令,将图片链接绑定到一个变量上。例如: ``` <img v-bind:src="imageUrl" v-on:error="handleImageError"> ``` 2. 在data钩子函数,定义一个默认图片的链接,并将其赋值给imageUrl变量。例如: ``` data() { return { imageUrl: '默认图片链接' } }, ``` 3. 在methods钩子函数,创建一个名为handleImageError的方法,用于处理图片加载错误。这个方法会在图片加载错误时被触发。例如: ``` methods: { handleImageError() { this.imageUrl = '默认图片链接'; } } ``` 4. 当图片加载错误时,handleImageError方法会被调用,将默认图片的链接赋值给imageUrl变量。这样就能在图片渲染404显示默认图片。 以上就是在Vue如何添加默认图片的步骤。通过使用v-bind指令将图片链接绑定到变量上,并在v-on指令添加一个错误处理方法,即可实现在图片渲染404显示默认图片的功能。 ### 回答3: 在Vue,当image图片加载失败时,可以通过添加默认图片来替代展示。 首先,在data定义一个默认图片的URL地址: ```javascript data() { return { defaultImageUrl: '/path/to/default-image.png' } } ``` 然后,在<img>标签使用v-bind指令绑定一个属性,使用三元表达式来判断图片地址是否为404,如果是则使用默认图片URL: ```javascript <template> <div> <img v-bind:src="imageUrl" v-bind:alt="imageAlt" v-bind:title="imageTitle" /> </div> </template> <script> export default { data() { return { imageUrl: '/path/to/image.png', // 图片地址 imageAlt: 'Image Alt', // 图片alt属性 imageTitle: 'Image Title' // 图片title属性 } }, methods: { handleImageError(event) { if (event.target && event.target.src !== this.defaultImageUrl) { event.target.src = this.defaultImageUrl; } } }, mounted() { this.$nextTick(() => { const image = this.$el.querySelector('img'); if (image) { image.addEventListener('error', this.handleImageError); } }); }, beforeDestroy() { const image = this.$el.querySelector('img'); if (image) { image.removeEventListener('error', this.handleImageError); } } } </script> ``` 在上述代码,handleImageError()为图片加载错误的回调函数,当图片加载失败时,该函数会将图片的src属性替换为默认图片的URL地址。 最后,在mounted钩子函数,通过querySelector获取到img元素,并给它绑定error事件,在事件触发时执行handleImageError方法。在beforeDestroy钩子函数,移除error事件的绑定,防止内存泄漏。 这样,当图片加载失败时,会自动替换成默认图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值