背景:
页面有多张图片,动态的设置图片的链接src, 即: :src

解决方法:

<template>
  <div>
     <img v-for="for group in groups" :src="exportImgSrc(group.url)">
   </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
let groups = [
{"url": "zicky-home-ride11.jpg","tag":"LIFE","title":"SUNRISE","content":"It is a nice day", "location":"Beijing", "date": "2022/02/02"} ,
{"url": "home-reading.jpg","tag":"LIFE","title":"SUNRISE","content":"It is a nice day", "location":"Beijing", "date": "2022/02/02"},
{"url": "nlp-meeting.jpg","tag":"LIFE","title":"SUNRISE","content":"It is a nice day", "location":"Beijing", "date": "2022/02/02"},
{"url": "reader.jpg","tag":"LIFE","title":"SUNRISE","content":"It is a nice day", "location":"Beijing", "date": "2022/02/02"}
]

const exportImgSrc = (name: string): string => {
    return new URL(`../../assets/moments/${name}`, import.meta.url).href;
};

</script>

此处, 一定要写为: ${name}。 此处如果图片地址为静态的(写死了地址),编译后,代码中的图片地址为静态的,编译后的图片地址带有hash, 导致找不到图片,前端就无法显示图片。

有关图片处理见说明:
https://vite-pwa-org.netlify.app/guide/static-assets

GitHub 加速计划 / vu / vue
207.55 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:3 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 5 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐