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

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

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:

代码部分不翻译。


<details>
<summary>英文:</summary>

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;

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>
</>
)
}


</details>


# 答案1
**得分**: 0

在使用像图片这样的静态资产在 *Vite* 中时,有多种解决方案可用。一种方法是使用 `new URL` 函数。

```js
export function Headerr() {
    const jose = new URL('../img/react-logo.png', import.meta.url).href

    return(
        <>
            <div className="redes">
                <img src={jose} alt="" />
            </div>
            <h1>Pizza</h1>
        </>
    )  
}

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

英文:

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

export function Headerr() {
    const jose = new URL(&#39;../img/react-logo.png&#39;, import.meta.url).href

    return(
        &lt;&gt;
            &lt;div className=&quot;redes&quot;&gt;
                &lt;img src={jose} alt=&quot;&quot; /&gt;
            &lt;/div&gt;
            &lt;h1&gt;Pizza&lt;/h1&gt;
        &lt;/&gt;
    )  
}

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:

确定