解决vue3 + vite + ts 中require失效的问题(require is not defind)

require is not defind因为require是属于webpack的方法,vite中找不到这个方法肯定报错

解决办法

通过vite官网了解到新的引入方式,我使用了其中一种

复制代码
imgList: [
        {
          name: "lj",
          src: new URL('../../assets/img/applyList.png', import.meta.url).href,
          title: "这是lj.png"
        },
        {
          name: "logo",
          src: new URL('../../assets/img/applyList.png', import.meta.url).href,
          title: "这是logo.png"
        },
        {
          name: "bg",
          src: new URL('../../assets/img/applyList.png', import.meta.url).href,
          title: "这是bg.png"
        },
        {
          name: "sadmas",
          src: new URL('../../assets/img/applyList.png', import.meta.url).href,
          title: "这是sadmas.png"
        }
      ]

创建一个js

创建方法:创建一个工具文件getImge.ts

复制代码
// 获取assets静态图片
export const getAssetsImge = (url: string) => {
  return new URL(`../assets/home/${url}`, import.meta.url).href
}

使用工具文件

复制代码
import { getAssetsImge } from '@/utils'

html

复制代码
          <el-avatar class="w-6 h-6" :src="getAssetsFile('user.png')" />
 
          <img class="w-6 h-6" :src="getAssetsFile('user.png')" alt="" /> 

借鉴文章:

vite+vue3中require is not defined-CSDN博客

解决vue3 + vite + ts 中require失效的问题(require is not defind)_vue3 require is not defined_夹道欢呼的博客-CSDN博客

相关推荐
谢尔登1 小时前
Vue3 响应式系统——computed 和 watch
前端·架构
愚公移码1 小时前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌
欣然~3 小时前
法律案例 PDF 批量转 TXT 工具代码
linux·前端·python
一个小废渣3 小时前
Flutter Web端网络请求跨域错误解决方法
前端·flutter
符文师4 小时前
css3 新特性
前端·css3
ct9784 小时前
WebGL开发
前端·gis·webgl
C_心欲无痕4 小时前
前端页面渲染方式:CSR、SSR、SSG
前端
果粒蹬i5 小时前
生成式 AI 质量控制:幻觉抑制与 RLHF 对齐技术详解
前端·人工智能·easyui
WordPress学习笔记6 小时前
解决Bootstrap下拉菜单一级链接无法点击的问题
前端·bootstrap·html
Never_Satisfied6 小时前
C#插值字符串中大括号表示方法
前端·c#