我在我的 React 項目中使用 Tailwind。我想在 div 中添加背景圖像,但它顯示以下錯誤:
Module not found: Error: Can't resolve '../../icons/blog-hero-1.png' in 'C:UsersDELLfrontend-developmentaidhumanity-practice-2src'
我正在添加順風類
bg-[url('../../icons/blog-hero-1.png')]
用于添加背景圖像,url 相對于當前文件,并且通過以下方式添加普通圖像時也可以工作:
import Hero from "../../icons/blog-hero-1.png" <div> <img src={Hero} className="h-full rounded-3xl"></img> </div>
誰能指導如何給出正確的網址? 注意:我在這里還添加了一個codesandbox示例,以便更好地演示,其中我嘗試在“Homepage.js”中導入背景圖像,但它不起作用。 https://codesandbox.io/s/background-image-wl9104?file=/src/components/Homepage.js
您也可以使用以下方法獲得相同的結果:
在您的 tailwind.config.js
文件中:
module.exports = { theme: { extend: { backgroundImage: { 'hero': "url('../../icons/blog-hero-1.png')" } }, }, plugins: [], }
您只需在類中提及 bg-hero
即可實現。