我有这个项目,我想使用我在app / javascript / images中拥有的某个背景图片(我所有的图片都在那里)。当我在HTML中使用使用它们时,它可以完美运行。但是当我尝试这样做时:
header.masthead {
padding-top: 10rem;
padding-bottom: calc(10rem - 4.5rem);
background-position: center;
background-repeat: no-repeat;
background-attachment: scroll;
background-size: cover;
background: linear-gradient(to bottom, rgba(92, 77, 66, 0.8) 0%, rgba(92, 77, 66, 0.8) 100%), image-url('images/singapore.jpg');
}
它根本不会加载...当我检查代码时,它说找不到模块。我怀疑这是因为webpack不能从CSS编译图像,尽管当我使用image_pack_tags时它是从我的html代码中编译的。
Javascript目录:
├── stylesheets
│ ├── application.scss
│ └── style.scss
├── packs
│ ├── application.js
│ └── script.js
└── images
└── singapore.jpg
└── ...
application.js:
const images = require.context('../images', true);
const imagePath = (name) => images(name, true);
因此,基本上,我将图像切换到资产管道并使用了它:
asset-url('photo.jpg')
仅此而已。