为什么我的React Bootstrap Glyphicons不显示?

问题描述 投票:12回答:1

我正在使用React Bootstrap的Glyphicon组件,但没有一个字形显示。到目前为止,这是我的代码:

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Global from './components/Global';

ReactDOM.render(
    <Global />, 
    document.getElementById('root')
);

Global.js

import React, { Component } from 'react';
import { FormGroup, FormControl, InputGroup, Glyphicon } from 'react-bootstrap';

class Global extends Component {
    render() {
        return(
          <div>
            <h2> Book Explorer!</h2>
              <FormGroup>
              <InputGroup>
                <FormControl
                  type="text"
                  placeholder="Search for a book"
                />
              <InputGroup.Addon>
                <Glyphicon glyph="search"></Glyphicon>
              </InputGroup.Addon>
            </InputGroup>
            </FormGroup>
          </div>
        )
    }
}

export default Global;

这是我的package.json文件:

{
  "name": "setup",
  "version": "1.0.0",
  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  },
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "start": "webpack-dev-server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.22.1",
    "babel-loader": "^6.2.10",
    "babel-polyfill": "^6.22.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.24.1",
    "react": "^15.5.4",
    "react-bootstrap": "^0.31.0",
    "react-dom": "^15.5.4",
    "webpack": "^1.14.0",
    "webpack-dev-server": "^1.16.2"
  }
}
javascript reactjs react-bootstrap glyphicons
1个回答
16
投票

这是因为Bootstrap使用字体Gylphicon Halflings来显示其字形。必须明确包含Bootstrap的CSS,因为它会导入glyphicons,否则glyphicons将不会显示。您可以将bootstrap.min.css文件添加到你的index.html文件中:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

关于这个问题,raised on GitHub存在问题。它没有明确表示你需要包含Bootstrap的CSS,我之前遇到过这个问题。您必须包含CSS文件,否则将不会导入glyphicons,您的组件将不显示任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.