我想从 sass 文件设置一个带有覆盖层的简单背景图像到react组件,但是我收到这个错误Module not found: You attempted to import ../assets/hero.jpg which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
,这很奇怪,因为包含图像的资产文件夹实际上在/src
文件夹内,这是我的文件夹结构体
src/
|assets/
hero.jpg
|sass/
|main.scss
|sassFiles/
|components.scss
|..
|components/
|Navbar.js
|Home.js
|..
|App.js
|index.js
|..
/* _components.scss file */
.hero {
height: 60vh;
background: linear-gradient(rgba(238, 238, 238, 0.459)), url(../../assets/hero.jpg) no-repeat 50% 50%;
background-size: cover;
}
当我像这样从组件导入图像时import hero from '../assets/hero.jpg'
,它工作正常,只有当我从 .scss 文件导入它时才会发生错误,除此之外,样式应用没有问题。