npm 包中的 CSS 图像 url

发布于 2025-01-17 09:48:30 字数 617 浏览 5 评论 0原文

我有一个在我的主要 webpack 构建应用程序中使用的包。在包中,我有一个组件及其相应的 .scss 文件。文件夹结构为:

component
   |- assets
       |- background-image.svg
   component.tsx
   component.scss

component.scss 尝试使用 background-image ,如下所示:

background: url('assets/bacgkround-image.svg')< /code>

这在故事书文件中运行良好,并且背景已正确加载。

但是,当我尝试将该组件包含在我的主 React 应用程序中时,构建失败并出现以下错误:

Module not found: Error: Can't resolve './assets/background-image.svg' in '<path-to-my-package-directory'

我很困惑。将相对 url 中的图像包含在 css 文件中的正确方法是什么?

I have a package that I am using in my main webpack-built app. In the package, I have a component with its corresponding .scss file. The folder structure is:

component
   |- assets
       |- background-image.svg
   component.tsx
   component.scss

component.scss is trying to use background-image as such:

background: url('assets/bacgkround-image.svg')

This works fine in a storybook file and the background is loaded properly.

However, when I try to include the component in my main React app, the build fails with the following error:

Module not found: Error: Can't resolve './assets/background-image.svg' in '<path-to-my-package-directory'

I am confused. What is the right way to include the image from a relative url in a css file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文