VUE 3的要求未针对IMG SRC定义
当我在vue 3中使用要求时,在vue 2上所有作品
<img :src="require('./img/1.png')" />
我都会出现错误:
[Vue warn]: Unhandled error during execution of render function
at <Creator key=1 >
at <Character>
at <App>
Uncaught ReferenceError: require is not defined
at Proxy._sfc_render (creator.vue:14:24)
at renderComponentRoot (runtime-core.esm-bundler.js:895:44)
at ReactiveEffect.componentUpdateFn [as fn] (runtime-core.esm-bundler.js:5059:57)
at ReactiveEffect.run (reactivity.esm-bundler.js:185:25)
at setupRenderEffect (runtime-core.esm-bundler.js:5185:9)
at mountComponent (runtime-core.esm-bundler.js:4968:9)
at processComponent (runtime-core.esm-bundler.js:4926:17)
at patch (runtime-core.esm-bundler.js:4518:21)
at mountChildren (runtime-core.esm-bundler.js:4714:13)
at mountElement (runtime-core.esm-bundler.js:4623:17)
只需文本文本文本
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解 vite文档正确,您是正确的,您应该能够做这样的事情:
If I understand the Vite documentation correctly, you should be able to do something like this:
requient
是WebPack特定的功能,可以处理资产导入。使用vite,它做的不同:
带有Vue Vite插件(您当然正在使用),您可以只使用
内部的相对或绝对路径
:src =“”
Vite将其转换为引擎盖下的动态导入。因此,这对您来说是透明的。require
is a webpack specific feature to handle assets import.Using vite, it's done differently: https://vitejs.dev/guide/assets.html#importing-asset-as-url
With the vue vite plugin (that you certainly are using), you can just use a relative or absolute path inside
:src=""
and vite will convert it to a dynamic import under the hood. So it's transparent for you.我刚刚切换到vue 3的webpack,需要再次工作
i just switched to webpack for vue 3 and require worked again