VUE 3的要求未针对IMG SRC定义

发布于 2025-02-09 17:47:11 字数 982 浏览 1 评论 0 原文

当我在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)

只需文本文本文本

When i use require in Vue 3, on vue 2 all works

<img :src="require('./img/1.png')" />

I get error:

[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)

Just text text text text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪菊怪哟 2025-02-16 17:47:11

如果我理解 vite文档正确,您是正确的,您应该能够做这样的事情:

<img :src="imageSrc" />
...
<script>
...
export default {
  ...
  computed: {
    imageSrc() {
      return new URL(`./img/${selectedItem}.png`, import.meta.url).href;
    }
  }
}
</script>

If I understand the Vite documentation correctly, you should be able to do something like this:

<img :src="imageSrc" />
...
<script>
...
export default {
  ...
  computed: {
    imageSrc() {
      return new URL(`./img/${selectedItem}.png`, import.meta.url).href;
    }
  }
}
</script>
对你而言 2025-02-16 17:47:11

requient 是WebPack特定的功能,可以处理资产导入。

使用vite,它做的不同:

带有Vue Vite插件(您当然正在使用),您可以只使用内部的相对或绝对路径:src =“” Vite将其转换为引擎盖下的动态导入。因此,这对您来说是透明的。

<img src="./imgs/cat.jpeg" width="300px" height="50px">
<img src="~/assets/dog.jpg" width="300px" height="50px">

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.

<img src="./imgs/cat.jpeg" width="300px" height="50px">
<img src="~/assets/dog.jpg" width="300px" height="50px">
∞梦里开花 2025-02-16 17:47:11

我刚刚切换到vue 3的webpack,需要再次工作

i just switched to webpack for vue 3 and require worked again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文