vite 不支持requrie require is not defined

vite 動態(tài)引入圖片的時候會報錯, require is not defined

網(wǎng)上很多推薦的是寫法是配置vite-plugin-require-transform
但是這個不是穩(wěn)定,我安裝配置后一直報錯
[vite] Internal server error: This experimental syntax requires enabling one of the following parser plugin(s): "jsx", "flow", "typescript". (1:0)


image.png

去插件githu查看issue,暫無法解決


image.png

放棄了,不再掙扎了。

替換成下面的方法進(jìn)行引入
1.new URL

const loadingName = (name) => {
  return new URL(`../src/assets/${name}.svg`, import.meta.url).href;
};

<img alt="Vue logo" class="logo" :src="url2" width="125" height="125" />

2.import

const loading = async (name) => {
 const src = await import(`/src/assets/${name}.svg`);
 url.value = src.default;
 return src.default;
};
<img
     alt="Vue logo"
     class="logo"
     :src="loading('logo')"
     width="125"
     height="125"
   />

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容