img使用 :src 动态绑定图片地址,图片不成功

使用vue cli 创建的vue2项目,项目中想实现轮播效果。

出现的问题:

使用 :src 动态绑定图片地址,图片没有出现

          <el-carousel :interval="3000" arrow="always">
            <el-carousel-item v-for="(item,index) in swipperImg" :key="index">
             <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
 data() {
    return {
      swipperImg:[
        {
          imgUrl:'../../assets/animals1.jpeg'
        },
        {
          imgUrl:'../../assets/animals2.jpg'
        },
        {
          imgUrl:'../../assets/animals4.jpg'
        },
        {
          imgUrl:'../../assets/animals5.jpg'
        },
        {
          imgUrl:'../../assets/animals7.jpeg'
        },
      ]
    }
  },

此时图片没有出现,控制台报错 http://localhost:8080/assets/animals1.jpeg 404 (Not Found)

当图片地址写死时,图片出现:

          <el-carousel :interval="5000" arrow="always">
            <el-carousel-item >
             <img src="../../assets/animals1.jpeg" alt="">
            </el-carousel-item>
          </el-carousel>

此时控制台中出现的图片地址是http://localhost:8080/img/animals1.dc0540de.jpeg

解决办法:

使用require引入图片,完整代码如下,此时图片引入成功

<template>
  <div class="">
        <div class="pt100 carousel">
          <el-carousel :interval="5000" arrow="always">
            <el-carousel-item v-for="(item,index) in swipperImg" :key="index">
             <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </div>
  </div>
</template>

<script>
export default {
  name:"",
  data() {
    return {
      swipperImg:[
        {
          imgUrl: require('../../assets/animals1.jpeg')
        },
        {
          imgUrl: require('../../assets/animals2.jpg')
        },
        {
          imgUrl: require('../../assets/animals4.jpg')
        },
        {
          imgUrl: require('../../assets/animals5.jpg')
        },
        {
          imgUrl: require('../../assets/animals7.jpeg')
        },
      ]
    }
  },
}
</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于`img`标签的`src`属性双向绑定报错,可能是因为Vue.js版本问题。在Vue.js 2.x版本,`img`标签的`src`属性双向绑定需要使用`.sync`修饰符,示例代码如下: ``` <template> <div> <img :src.sync="imageUrl"> <button @click="changeUrl">Change Image URL</button> </div> </template> <script> export default { data() { return { imageUrl: 'https://example.com/image.jpg' } }, methods: { changeUrl() { this.imageUrl = 'https://example.com/new-image.jpg'; } } } </script> ``` 如果你使用的是Vue.js 3.x版本,`img`标签的`src`属性双向绑定需要使用`v-model`指令,示例代码如下: ``` <template> <div> <img v-model="imageUrl"> <button @click="changeUrl">Change Image URL</button> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const imageUrl = ref('https://example.com/image.jpg'); const changeUrl = () => { imageUrl.value = 'https://example.com/new-image.jpg'; } return { imageUrl, changeUrl } } } </script> ``` 另外,也可以通过在`img`标签使用`key`属性来强制重新加载图片,例如: ``` <template> <div> <img :src="imageUrl" :key="imageUrl"> <button @click="changeUrl">Change Image URL</button> </div> </template> <script> export default { data() { return { imageUrl: 'https://example.com/image.jpg' } }, methods: { changeUrl() { this.imageUrl = 'https://example.com/new-image.jpg'; } } } </script> ``` 每次更新`imageUrl`的值时,`img`标签的`key`属性也会更新,从而强制重新加载图片

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值