【Vue】(解决)el-image 图片引入根路径@无效

本文介绍了在Vue应用中遇到图片加载失败的问题,通过使用require()函数正确引入本地资产图片的方法,帮助开发者解决常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图片在根路径下,如下引用图片路径发现无效,

<el-image :src="imgs"></el-image>


 data() {
    return {
    	imgs:"@/assets/imgs/achievement.png"
    }
}

图片显示 “加载失败”



解决方法:使用require()
Require函数: 用于引入模块、 JSON、或本地文件。 可以从 node_modules 引入模块。

require是运行时调用,所以require理论上可以运用在代码的任何地方

<el-image :src="imgs"></el-image>


 data() {
    return {
    	imgs:require("@/assets/imgs/achievement.png")
    }
}

这样就能正常引入图片了

<!-- src/components/ImageCropper.vue --> <template> <div> <el-upload action="#" accept="image/*" :show-file-list="false" :auto-upload="false" :on-change="handleUpload" > <el-button type="primary">选择图片</el-button> </el-upload> <div v-if="imageSrc" class="mt-4"> <vue-cropper ref="cropperRef" :src="imageSrc" :aspect-ratio="aspectRatio" :view-mode="viewMode" :guides="true" ></vue-cropper> <div class="mt-4 flex gap-2"> <el-button @click="rotate(-90)">逆时针旋转</el-button> <el-button @click="rotate(90)">顺时针旋转</el-button> <el-button type="success" @click="cropImage">裁剪图片</el-button> </div> </div> <div v-if="croppedImage" class="mt-4"> <h4>裁剪结果:</h4> <img :src="croppedImage" class="result-image" /> <el-button type="primary" @click="downloadImage" class="mt-2">下载图片</el-button> </div> </div> </template> <script setup> import { ref } from 'vue'; import { VueCropper } from 'vue-cropper'; import 'vue-cropper/dist/index.css'; import { ElMessage } from 'element-plus'; const cropperRef = ref(null); const imageSrc = ref(''); const croppedImage = ref(''); const aspectRatio = ref(16 / 9); // 默认宽高比 const viewMode = ref(1); // 0-无限制, 1-限制裁剪框不超过图片 // 处理图片上传 const handleUpload = (file) => { if (!file.raw.type.match('image.*')) { ElMessage.error('请选择图片文件'); return; } const reader = new FileReader(); reader.onload = (e) => { imageSrc.value = e.target.result; croppedImage.value = ''; // 清除之前的裁剪结果 }; reader.readAsDataURL(file.raw); }; // 旋转图片 const rotate = (deg) => { cropperRef.value.rotate(deg); }; // 执行裁剪 const cropImage = () => { cropperRef.value.getCroppedCanvas().toBlob((blob) => { const reader = new FileReader(); reader.onload = () => { croppedImage.value = reader.result; }; reader.readAsDataURL(blob); }); }; // 下载图片 const downloadImage = () => { const link = document.createElement('a'); link.href = croppedImage.value; link.download = 'cropped-image.png'; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; </script> <style scoped> .result-image { max-width: 100%; border: 1px solid #eee; margin-top: 10px; } </style> 执行选择图片都没有图片显示的,点击裁剪会报chunk-LKTAM6RV.js?v=5230a3fd:1733 Uncaught TypeError: cropperRef.value.getCroppedCanvas is not a function
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北海南风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值