【问题标题】:How to load background images from css in React and NextJS如何在 React 和 NextJS 中从 css 加载背景图像
【发布时间】:2019-08-24 12:47:11
【问题描述】:

图像没有从用于背景的 CSS 文件加载,但在 jsx 中它正在加载

在 CSS 文件中,我使用如下

.main-banner {
  position: relative;
  height: 910px;
  z-index: 1;
  background: transparent url(../../static/images/banner-bg1.jpg) right top no-repeat;
}

图片网址绝对没问题

还有这样的配置文件

//next.config.js
const withImages = require('next-images');
const withCSS = require('@zeit/next-css')

module.exports = withImages(withCSS({
    cssLoaderOptions: {
        url: false
    }
}))

究竟是什么问题?

谢谢

【问题讨论】:

标签: css reactjs next.js


【解决方案1】:

我在 css prop background-image 中收到了 [object Module]。 url-loader 选项中的标志esModule: false 修复了这个问题。

const withCSS = require('@zeit/next-css');
const nextConfig = withCSS({
    webpack(config, options) {
        config.module.rules.push({
            test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
            use: {
                loader: 'url-loader',
                options: {
                    limit: 100000,
                    esModule: false,
                    name: '[name].[ext]'
                }
            }
        });
        return config;
    }
});

【讨论】:

    【解决方案2】:

    您的静态文件正在部署到网络的根目录。和你编译的js和css文件一样。

    因此您可以使用以下网址访问它们:url(/static/images/banner-bg1.jpg)

    this github 问题上获得更多信息。

    【讨论】:

    猜你喜欢
    • 2021-05-26
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2015-01-14
    • 2021-03-31
    • 2021-11-07
    相关资源
    最近更新 更多