[英]React doesn't display images placed in public folder when I install gh-pages but when I push the code it displays in gh-pages?
[英]React doesn't display images placed in public folder when deployed to gh-pages but works in local host, why?
我構建了一個簡單的應用程序,它在 react 的公共文件夾中呈現三張包含圖像的卡片。 它在localhost:3000
工作,但是當我試圖將它部署到 GitHub 時,gh-pages 圖像會中斷。 它似乎沒有從公共文件夾中獲取圖像。
我遵循的步驟部署到 gh-pages:
在 package.json 中為主頁創建了一個鍵值對,並在腳本部分添加了這兩行
"predeploy":"npm run build"
, "deploy":"gh-pages -d build"
,
npm install gh-pages --save-dev
承諾並推送了上述所有更改
npm run deploy
這 4 個步驟成功部署了我的應用程序
https://sandeep194920.github.io/CompoundComponents/但是,正如您所看到的,圖像沒有顯示在本地主機中。
讓我知道我在這里犯了什么錯誤。 謝謝你。
生成的 HTML 將鏈接到這樣的圖像
<p><img alt="" src="/img/image.png" /></p>
但圖像實際上會在這里。
所以這個問題可以通過使用相對路徑來避免,如果存儲庫名稱應該自動添加到圖像 URL 之前,如果它是絕對路徑,這是值得的。
可以嘗試以下方法:
![](./img/image.svg)
![](img/image.svg)
<img src="./img/image.svg" />
<img src="img/image.svg" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.