如何在react.js项目的bootstrap导航栏中放置glyphicon

时间:2017-04-12 21:01:42

标签: javascript reactjs twitter-bootstrap-3

如何在bootstrap导航栏中显示glyphicon?我有一个React.js项目,我一直在努力,我想将GitHub glyphicon重新定位到导航栏。到目前为止,我的内容如下所示。github glyphicon

App.js

中的导航栏相关的代码
import React, {PropTypes} from 'react';
import NavBar from './common/NavBar-test';
import FontAwesome from 'react-fontawesome';

var navbar = {};
navbar.brand =  {linkTo: "#", text: "app"};
navbar.links = [

  {linkTo: "#Demonstration", text: "Demonstration"},
  {linkTo: "#Demonstration2", text: "Demonstration #2"},
  {linkTo: "https://github.com/user/app", text:"GitHub Source Code"},
  {linkTo: "https://itunes.apple.com/us/app/", text: "App Store"},
  {linkTo: "https://github.com/user/appr", text: "The Future of app"}
];

Navbar-test.js

import React, { PropTypes } from 'react';

// create classes
var NavBar = React.createClass({
  render: function(){
    return(
      <nav className="navbar navbar-default navbar-static-top">
        <div className="container-fluid">
          <div className="navbar-header">
            <button type="button" className="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false">
              <span className="sr-only">Toggle navigation</span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
            </button>
            <NavBrand linkTo={this.props.brand.linkTo} text={this.props.brand.text} />
          </div>
          <div className="collapse navbar-collapse" id="navbar-collapse">
            <NavMenu links={this.props.links} />
          </div>
        </div>
      </nav>
    );
  }
});

var NavBrand = React.createClass({
  render: function(){
    return (
      <a className="navbar-brand" href={this.props.linkTo}>{this.props.text}</a>
    );
  }
});

var NavMenu = React.createClass({
  render: function(){
    var links = this.props.links.map(function(link){
      if(link.dropdown) {
        return (
          <NavLinkDropdown key={link.text} links={link.links} text={link.text} active={link.active} icon={link.icon} />
        );
      }
      else {
        return (
          <NavLink key={link.text} linkTo={link.linkTo} text={link.text} active={link.active} />
        );
      }
    });
    return (
      <ul className="nav navbar-nav">
        {links}
      </ul>
    );
  }
});

var NavLinkDropdown = React.createClass({
  render: function(){
    var active = false;
    var links = this.props.links.map(function(link){
      if(link.active){
        active = true;
      }
      return (
        <NavLink key={link.text} linkTo={link.linkTo} text={link.text} active={link.active} />
      );
    });
    return (
      <li className={"dropdown " + (active ? "active" : "")}>
        <a href="#" className="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
          {this.props.text}
          <span className="caret"></span>
        </a>
        <ul className="dropdown-menu">
          {links}
        </ul>
      </li>
    );
  }
});

var NavLink = React.createClass({
  render: function(){
    return(
      <li className={(this.props.active ? "active" : "")}>
        <a href={this.props.linkTo}>{this.props.text}{this.props.icon}</a>  
      </li>
    );
  }
});

export default NavBar;

理想情况下,我希望将GitHub glyphicon放在说明 GitHub源代码的文本前面。

2 个答案:

答案 0 :(得分:1)

看起来你正在映射链接(这很酷),但你可以在映射的链接之前添加你想要的GitHub数据。

你最终可能会遇到这样的事情:

<span>
    <NavLink>GitHub Source Code</NavLink>
    <GlyphIcon />
</span>
// Then map over links here

答案 1 :(得分:0)

简而言之,我终于得到了字形,以显示我希望他们如何。proof in the pudding

我需要修改Navbar-test的来源以支持图标。

类似的东西,

class NavMenu extends React.Component {
  render() {
    var links = this.props.links.map(function(link){
      if(link.dropdown) {
        return (
          <NavLinkDropdown key={link.text} links={link.links} text={link.text} active={link.active} icon={link.icon} />
        );
      }
      else {
        return (
          <NavLink key={link.text} linkTo={link.linkTo} icon={link.icon} text={link.text} active={link.active}  />
        );
      }
    });
    return (
      <ul className="nav navbar-nav">
        {links}
      </ul>
    ); // close return
  } // close render
} // close NavMenu