【问题标题】:How to set backgound image for a component in Next.js?如何在 Nextjs 中为组件设置背景图片?
【发布时间】:2020-06-04 08:10:00
【问题描述】:

我查看了这个文档,Static File Serving,它明确指出:

Next.js 可以在根目录中名为 public 的文件夹下提供静态文件,例如图像。然后,您的代码可以从基本 URL (/) 开始引用 public 中的文件。

我已经在public文件夹中放了一个名为background.jpg的图片文件。

然后在我的pages/referral.js 我有以下代码:

import React from 'react';
import backgroundImage from '/background.jpg'; // later I removed this 

const styles = {
    container: {
        backgroundImage: `url(${backgroundImage})`,
        backgroundPosition: 'center',
        backgroundSize: 'cover',
        backgroundRepeat: 'no-repeat',
        width: '100vw',
        height: '100vh'
    }
};

export default class Referral extends React.Component{

    render(){
        return(
            <div className="cComponent" style={styles} >
                <h1 align="center">Some item</h1>
            </div>
        )
    }
}

然后我收到此错误:Module not found: Can't resolve '/background.jpg' in referral.js

我尝试了什么:

  1. const backgroundImage = require('/background.jpg'); <-- and tried this
  2. import backgroundImage from '/public/background.jpg';
  3. import backgroundImage from './background.jpg'

上述所有尝试也会得到相同的错误。

我什至尝试添加next.config.js

// next.config.js 
const withImage = require('next-images');
module.exports=withImage({});

然后像这样导入pages/referral.js

 import backgroundImage from '/background.jpg';

也会出现同样的错误:Module not found: Can't resolve '/background.jpg' in referral.js

我在这里错过了什么?也有人可以给我一个关于如何将背景图像放入组件的示例吗?

【问题讨论】:

  • const backgroundImage = require('public/background.jpg')
  • @bcjohn 先生同样的错误。 Module not found: Can't resolve '/public/background.jpg'

标签: javascript next.js


【解决方案1】:

图片不需要导入,浏览器指定路径即可。

假设你有/public/bg.jpg,你可以在代码中引用它

backgroundImage: 'url(/bg.jpg)'

【讨论】:

    【解决方案2】:

    我认为你需要改变你的风格,因为你的容器有你的 css 可以应用。 style={styles.container}

    <div className="cComponent" style={styles.container} >
       <h1 align="center">Some item</h1>
    </div>
    

    您可以检查和更改您的样式 css

    <div>
          Hello
          <div
            style={{
              backgroundImage: `url(${Image})`,
              backgroundSize: "cover",
              backgroundRepeat: "no-repeat",
              width: "100vw",
              height: "100vh"
            }}
          />
          <hr />
          <div
            style={{
              backgroundImage: `url(/kittycat.jpg)`,  // coming from public folder
              backgroundSize: "cover",
              backgroundRepeat: "no-repeat",
              width: "100vw",
              height: "100vh"
            }}
          />
        </div>
    

    供您参考Live working demo

    【讨论】:

    • 先生,我在公共文件夹中使用 next.js ,我的 backgound.png ,但它一直给我这个错误:Module not found: Can't resolve './background.jpg' 我不知道为什么..即使我对你的回答也完全一样
    • @ken 现在您可以查看实时工作演示...我也为公共文件夹创建了...希望对您有所帮助。
    • 先生,它现在可以工作了!!!!!!现在的问题是,为什么它有效?当我更改为backgroundImage: url(/background.jpg),
    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2017-12-01
    相关资源
    最近更新 更多