vue中引入绝对路径报错的解决办法:1、使用“await import(’@/assets/img/22.png’);”方式引入路径;2、循环利用返回值请求本地图片即可。
本教程操作环境:Windows10系统、vue3版、DELL G3电脑
vue中引入绝对路径报错怎么办?
vue3+vite :src 用require引入绝对路径报错
最近的项目是vue3+vite,在使用require引用图片路径的时候就报错 require is not defined,就很尴尬,因为typescript不支持require所以之前直接用imgUrl: require(’…/assets/test.png’) 导入就会报错需要用import导入,记录一下解决方法:
立即学习“前端免费学习笔记(深入)”;
第一种:使用await import(’@/assets/img/22.png’);
<template> @@##@@ </template> <script> import {ref, onMounted} from "vue"; export default { name: "imgPage", setup(){ onMounted(()=>{ handleImgSrc(); }) const imgUrl = ref(''); const handleImgSrc = async()=>{ let m = await import('@/assets/img/22.png'); imgUrl.value = m.default; }; return{ imgUrl } } } </script>
第二种:循环利用返回值请求本地图片
<template> @@##@@ </template> <script> import {ref, reactive, onMounted} from "vue"; export default { name: "imgPage", setup(){ const imgList = reactive([ {url: 'a.png'},{url: 'b.png'},{url: 'c.png'} ]) const getAssetsImages =(name)=> { return new URL(`/src/assets/pic/${name}`, import.meta.url).href; //本地文件路径 } return{ imgList , getAssetsImages } } } </script>
推荐学习:《vue视频教程》
以上就是vue中引入绝对路径报错怎么办的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号