【问题标题】:Reactjs - can't set a background image on Bootstrap's JumbotronReactjs - 无法在 Bootstrap 的 Jumbotron 上设置背景图像
【发布时间】:2017-06-18 21:11:04
【问题描述】:

图像没有出现,我不确定是语法有问题还是图像的路径有误。

import React, { Component } from 'react';
import { Jumbotron, Button } from 'react-bootstrap';
import { Link } from 'react-router-dom'


class Jumbo extends Component {
  render() {
    function myFunction() {
      console.log('test');
    }

    return (
      <Jumbotron style={styles.container}>
        <h1>{this.props.title}</h1>
        <p>In a town like Twin Peaks no one is innocent</p>
        <Button
          bsStyle="primary"
          onClick={myFunction}
        >
          Learn more
        </Button>
        <Button
          bsStyle="primary"
        >
          <Link to='/contact'>Contact</Link>
        </Button>
      </Jumbotron>
    );
  }
}

const bgImage = '../images/twin.jpg';

const styles = {
    container: {
    backgroundImage: `url(${bgImage})`
  }
};
export default Jumbo;

“Jumbo”组件位于“Components”目录中。 'twin.jpg' 位于 'images' 目录中。请看下面的截图。

【问题讨论】:

    标签: twitter-bootstrap reactjs


    【解决方案1】:

    这很可能是路径问题 - 反应代码看起来正确。我不确定您的项目是如何设置的,但通常图像资产会进入您的public 目录。可能是public/images。然后你可以从你的网络服务器的根目录访问它们,路径可能是:

    url(/images/twin.jpg)

    先尝试将图片放入公用文件夹,然后尝试通过浏览器 URL 直接访问。

    【讨论】:

    • 就是这样。我不知道它应该在公共场合。现在你指出来了,这是有道理的。
    【解决方案2】:

    您应该使用 webpack 来构建项目,然后当您导入图像时,您将拥有该图像 www 或公共文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 2016-12-13
      • 2015-02-15
      • 1970-01-01
      • 2017-07-20
      • 2014-09-13
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多