0
点赞
收藏
分享

微信扫一扫

将后端给的数据转换成el-cascader面板能渲染的数据格式

闲云困兽 03-23 21:00 阅读 2

最近在复习插槽有关知识的时候,遇到一个问题。

我想把本地图片动态的展示到页面上,却实现不了。😭😭😭😭😭😭😭😭😭😭😭😭

后来搜索了很多有关这方面的知识,才得到了解决。🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳🥳

展示错误情况

(一)直接使用相对路径

<template>
  <div class="">
    <Child :arr="list">
      <template v-slot="slotProps">
        <img width="100px" height="100px" :src="slotProps.row.avatar" alt="" />
      </template>
    </Child>
  </div>
</template>

<script>
import Child from './components/child.vue'

export default {
  data() {
    return {
      list: [
        {
          name: '小明',
          avatar: '@/assets/剑士.jpg'
        },
        {
          name: '小红',
          avatar: '@/assets/路飞.jpeg'
        },
        {
          name: '小刚',
          avatar: '@/assets/小黄人.jpg'
        }
      ]
    }
  },
  components: {
    Child
  }
}
</script>

<style scoped></style>

 报错:404,找不到:

(二)在 src 里面加上require 

<template>
  <div class="">
    <Child :arr="list">
      <template v-slot="slotProps">
        <img width="100px" height="100px" :src="require(slotProps.row.avatar)" alt="" />
      </template>
    </Child>
  </div>
</template>

<script>
import Child from './components/child.vue'

export default {
  data() {
    return {
      list: [
        {
          name: '小明',
          avatar: '@/assets/剑士.jpg'
        },
        {
          name: '小红',
          avatar: '@/assets/路飞.jpeg'
        },
        {
          name: '小刚',
          avatar: '@/assets/小黄人.jpg'
        }
      ]
    }
  },
  components: {
    Child
  }
}
</script>

<style scoped></style>

又报错,找不到模块:

正确使用 

在 avatar 里面加上 require:

<template>
  <div class="">
    <Child :arr="list">
      <template v-slot="slotProps">
        <img width="100px" height="100px" :src="slotProps.row.avatar" alt="" />
      </template>
    </Child>
  </div>
</template>

<script>
import Child from './components/child.vue'

export default {
  data() {
    return {
      list: [
        {
          name: '小明',
          avatar: require('@/assets/剑士.jpg')
        },
        {
          name: '小红',
          avatar: require('@/assets/路飞.jpeg')
        },
        {
          name: '小刚',
          avatar: require('@/assets/小黄人.jpg')
        }
      ]
    }
  },
  components: {
    Child
  }
}
</script>

<style scoped></style>

无报错:

举报

相关推荐

0 条评论