“Vite with React – 浏览器错误“Uncaught SyntaxError: ambiguous indirect export: jose””

huangapple go评论171阅读模式
英文:

Vite with react - Browser error "Uncaught SyntaxError: ambiguous indirect export: jose"

问题

"jose" export indirect ambiguous: SyntaxError Uncaught error console returns page inspected browser shown image no but JSX with image an render to trying I'm:

代码部分不翻译。

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to render an image with `JSX` but there&#39;s no image shown in the browser. I inspected the page and it returns this error on console &quot;Uncaught SyntaxError: ambiguous indirect export: jose&quot;
  4. Here&#39;s the code:

import { jose } from "../img/react-logo.png";
export function Headerr() {
return(
<>
<div className="redes">
<img src={jose} alt="" />
</div>
<h1>Pizza</h1>
</>
)
}

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 在使用像图片这样的静态资产在 *Vite* 中时,有多种解决方案可用。一种方法是使用 `new URL` 函数。
  5. ```js
  6. export function Headerr() {
  7. const jose = new URL('../img/react-logo.png', import.meta.url).href
  8. return(
  9. <>
  10. <div className="redes">
  11. <img src={jose} alt="" />
  12. </div>
  13. <h1>Pizza</h1>
  14. </>
  15. )
  16. }

这种方法允许你根据模块的位置动态生成正确的图片URL。

英文:

When working with static assets like images in Vite, there are multiple solutions available. One approach is to use the new URL function.

  1. export function Headerr() {
  2. const jose = new URL(&#39;../img/react-logo.png&#39;, import.meta.url).href
  3. return(
  4. &lt;&gt;
  5. &lt;div className=&quot;redes&quot;&gt;
  6. &lt;img src={jose} alt=&quot;&quot; /&gt;
  7. &lt;/div&gt;
  8. &lt;h1&gt;Pizza&lt;/h1&gt;
  9. &lt;/&gt;
  10. )
  11. }

This approach allows you to dynamically generate the correct URL for the image based on the module's location.

huangapple
  • 本文由 发表于 2023年6月2日 04:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定