vue3导入本地图片2种实现方法

  1. <script setup>中使用import语法:
javascript 复制代码
<template>
  <img :src="logo" alt="Logo">
</template>
 
<script setup>
import logo from './assets/logo.png';
</script>
  1. 使用Vue的ref来动态地在<script setup>中更换图片:

javascript 复制代码
<template>
  <img :src="imageSrc" alt="Dynamic Image">
  <button @click="changeImage">Change Image</button>
</template>
 
<script setup>
import { ref } from 'vue';
 
const imageSrc = ref('./assets/logo.png');
 
function changeImage() {
  imageSrc.value = './assets/other-image.png';
}
</script>
  1. 方法二:使用new URL(url, import.meta.url)
javascript 复制代码
<script setup>
import { ref } from "vue";
const Img = ref("");
Img.value = new URL(
  "@/assets/xxxx.jpg",
  import.meta.url
).href;
</script>
相关推荐
Qin_jiangshan几秒前
vue实现进度条带指针
前端·javascript·vue.js
天高任鸟飞dyz2 分钟前
tabs切换#
javascript·vue.js·elementui
菜鸟una7 分钟前
【layout组件 与 路由镶嵌】vue3 后台管理系统
前端·vue.js·elementui·typescript
小张快跑。8 分钟前
【Vue3】使用vite创建Vue3工程、Vue3基本语法讲解
前端·前端框架·vue3·vite
Zhen (Evan) Wang14 分钟前
.NET 8 API 实现websocket,并在前端angular实现调用
前端·websocket·.net
星空寻流年33 分钟前
css3响应式布局
前端·css·css3
Anesthesia丶1 小时前
Vue3 + naive-ui + fastapi使用心得
vue.js·ui·fastapi
Rverdoser1 小时前
代理服务器运行速度慢是什么原因
开发语言·前端·php
陌尘(MoCheeen)1 小时前
技术书籍推荐(002)
java·javascript·c++·python·go
航Hang*1 小时前
前端项目2-01:个人简介页面
前端·经验分享·html·css3·html5·webstorm