2016-10-22 89 views
39

我使用vue-cli創建了一個vue webpack項目。如何在vue.js webpack項目上正確設置favicon.ico?

vue init webpack myproject 

然後跑到下dev模式項目:

npm run dev 

我得到這個錯誤:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8080/favicon.ico

所以裏面的WebPack,如何正確導入favicon.ico

+1

你試過只是拖放到該站點的根目錄? :)或在公共生成文件夾? – Benjamin

回答

63

退房的WebPack模板的項目結構:https://vuejs-templates.github.io/webpack/structure.html

請注意,有一個靜態的文件夾,用node_modulessrc沿等

如果你把一些圖片到static文件夾,如favicon.png,它將提供在http://localhost:8080/static/favicon.png

這裏是靜態資產的文檔:https://vuejs-templates.github.io/webpack/static.html

爲了您的收藏夾圖標的問題,你可以把一個favicon.icofavicon.pngstatic文件夾,並指在您的index.html的<head>如下:

<head> 
    <meta charset="utf-8"> 
    <link rel="shortcut icon" type="image/png" href="/static/favicon.png"/> 
    <title>My Vue.js app</title> 
    ... 
</head> 

如果你沒有在你的index.html定義favicon.ico,那麼瀏覽器將從網站根目錄請求一個圖標(默認行爲)。如果你像上面那樣指定了一個favicon,你將不會再看到這個404。 favicon也將開始在您的瀏覽器標籤中顯示。

作爲一個側面說明,這裏就是爲什麼我喜歡的,而不是ICO文件PNG:

favicon.png vs favicon.ico - why should I use PNG instead of ICO?