为什么我的 React Bootstrap Glyphicons 没有显示?

Why aren't my React Bootstrap Glyphicons showing?

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

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"
  }
}

这是因为 Bootstrap 使用字体 Gylphicon Halflings 来显示其字形。 Bootstrap 的 CSS 必须明确包含,因为它会导入字形,否则字形将不会显示。您可以将 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 文件,否则将不会导入字形并且您的组件将不显示任何内容。

我认为 react-bootstrap 不包含字形组件, (我检查了那里的组件:https://react-bootstrap.github.io/components/alerts/

因此您需要在 react-bootstrap 包之上添加字形图标 :

在 index.html 文件中,添加以下行:

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">

这应该可以解决问题,而无需第二次导入整个 bootstrap css。

Bootstrap 在 v4 中放弃了 Glyphicon 支持 https://getbootstrap.com/docs/4.0/migration/#components