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>
相关推荐
行走的陀螺仪1 小时前
uni-app + Vue3编辑页/新增页面给列表页传参
前端·vue.js·uni-app
摘星编程2 小时前
React Native + OpenHarmony:Spinner旋转加载器
javascript·react native·react.js
We་ct2 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
2301_812731412 小时前
CSS3笔记
前端·笔记·css3
ziblog2 小时前
CSS3白云飘动动画特效
前端·css·css3
越努力越幸运5082 小时前
CSS3学习之网格布局grid
前端·学习·css3
半斤鸡胗3 小时前
css3基础
前端·css
ziblog3 小时前
CSS3创意精美页面过渡动画效果
前端·css·css3
akangznl3 小时前
第四章 初识css3
前端·css·css3·html5
会豪3 小时前
深入理解 CSS3 滤镜(filter):从基础到实战进阶
前端·css·css3