目标
我正在使用ReactJS,我正在尝试动态证明我的Nav / Navbar中的所有项目,如下面的示例所示。
问题:
我已经研究了一些选项,例如在Nav
标签上添加“justified”,但这最终将我的所有NavItems / MenuItem压缩在一起。我尝试将Nav
包裹在div
内,给它className
,然后尝试使用。{c}文件中的text-align: center
修改对齐方式。不幸的是,这没有改变对齐方式。
导航组件
/* Basic Functionality Imports */
import React from 'react';
import { render } from 'react-dom';
import { Navbar, Nav, NavItem, MenuItem, NavDropdown } from 'react-bootstrap';
import { BrowserRouter as Router, Route, Link, NavLink } from "react-router-dom";
import { getUserGroup } from './Auth.jsx';
/* All page imports */
import GenReports from '../pages/GenReports';
import IntlReports from '../pages/IntlReports';
var isDev = getUserGroup() === "dev" ? true : false;
var isAdmin = getUserGroup() === "admin" ? true : false;
var isDataAnalyst = getUserGroup() === "analyst" ? true : false;
/*
* Below is an example of conditional rendering
* Using the above variables as shorthand, each "render" performs a check against the user role
* to determine if the navigation bar should render that piece of the navigation component.
* The final navbar pushed to the component that utilizes the Navigation componenet will never
* know the existance of the non-rendered pieces, helping to prevent potential security issues
*/
var renderUserVerify = !isDataAnalyst ? <MenuItem eventKey={1.1}><NavLink to="/UserVerfication">USER VERIFICATION</NavLink></MenuItem> : null;
var renderPkgLookup = !isDataAnalyst ? <MenuItem eventKey={1.2}><NavLink to="/PkgInqueryn">PACKAGE INQUERY</NavLink></MenuItem> : null;
var renderQuerySearch = !isDataAnalyst ? <NavDropdown eventKey={1} title="QUERY SEARCHES" id="query-nav-dropdown">
{renderUserVerify}
{renderPkgLookup}
</NavDropdown> : null;
var renderUpdateUser = isDev || isAdmin ? <NavItem eventKey={2}><NavLink to="/UpdateUser">UPDATE USER</NavLink></NavItem> : null;
var renderGenReports = isDev || isAdmin || isDataAnalyst ? <MenuItem eventKey={3.1}><NavLink to="/reports/GenReports">GENERAL REPORTS</NavLink></MenuItem> : null;
var renderIntlReports = isDev || isAdmin || isDataAnalyst ? <MenuItem eventKey={3.2}><NavLink to="/reports/IntlReports">INTERNATIONAL REPORTS</NavLink></MenuItem> : null;
var renderReports = isDev || isAdmin || isDataAnalyst ? <NavDropdown eventKey={3} title="REPORTS" id="reports-nav-dropdown">
{renderGenReports}
{renderIntlReports}
</NavDropdown> : null;
var renderUserPref = !isDataAnalyst ? <NavItem eventKey={4}><NavLink to"/UserPref">USER PREFERENCE</NavLink></NavItem> : null;
export default class Navigation extends React.Component {
render() {
return (
<Router>
<div>
<Navbar inverse fluid collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
<><NavLink to="/"> HOME </NavLink>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav justified>
{renderQuerySearch}
{renderUpdateUser
{renderReports}
{renderUserPref}
<NavItem eventKey={5}><NavLink to="/Help">HELP</NavItem>
<NavItem eventKey={6}><NavLink to="/logout">LOGOUT</NavItem>
</Nav>
</Navbar.Collapse>
</Navbar>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
<Route path="/GenReports" component={GenReports} />
<Route path="/IntlReports" component={IntlReports} />
</div>
</Router>
);
}
}
const Home = () => (
<div>
<h2>I am your Overlord! Fear me!</h2>
</div>
);
const About = () => (
<div>
<h2>About</h2>
</div>
);
const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<NavLink to={`${match.url}/rendering`}>Rendering with React</NavLink><br />
<NavLink to={`${match.url}/components`}>Components</NavLink><br />
<NavLink to={`${match.url}/props-v-state`}>Props v. State</NavLink><br />
<Route path={`${match.url}/:topicId`} component={Topic} />
<Route
exact
path={match.url}
render={() => <h3>Please select a topic.</h3>}
/>
</div>
);
const Topic = ({ match }) => (
<div>
<h3>{match.params.topicId}</h3>
</div>
);
示例报告页面(目前看来都很相似)
import React from 'react';
export default class IntlReports extends React.Component {
render() {
return (
<h1>You have reached the International Reports Page</h1>
);
}
}
输出HTML
<html lang="en">
<head>
<meta charset="utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/favicon.ico">
<!--
Notice the use of in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link type="text/css" href="/node_modules/bootstrap/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<title>React App</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
.centerMe {
margin: 0;
float: none;
text-align: center;
}
</style>
<style type="text/css">
.App {
text-align: center;
}
.App-logo {
-webkit-animation: App-logo-spin infinite 20s linear;
animation: App-logo-spin infinite 20s linear;
height: 80px;
}
.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}
.App-title {
font-size: 1.5em;
}
.App-intro {
font-size: large;
}
@-webkit-keyframes App-logo-spin {
from { -webkit-transform: rotate(0deg); transform: rotate(0deg); }
to { -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}
@keyframes App-logo-spin {
from { -webkit-transform: rotate(0deg); transform: rotate(0deg); }
to { -webkit-transform: rotate(360deg); transform: rotate(360deg); }
}
</style>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"><div class="App">
<header class="App-header">
<img src="/static/media/logo.5d5d9eef.svg" class="App-logo" alt="logo">
<h1 class="App-title">Welcome to React</h1>
</header>
<div>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand active" aria-current="true" href="/"> HOME </a>
<button type="button" class="navbar-toggle collapsed">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="centerMe nav navbar-nav">
<li class="dropdown">
<a id="query-nav-dropdown" role="button" class="dropdown-toggle" aria-haspopup="true" aria-expanded="false" href="#">QUERY SEARCHES <span class="caret"></span></a>
<ul role="menu" class="dropdown-menu" aria-labelledby="query-nav-dropdown">
<li role="presentation" class="">
<a role="menuitem" tabindex="-1" href="#"><a aria-current="false" href="/PkgLookup">PACKAGE LOOKUP</a></a>
</li></ul></li>
<li role="presentation" class=""><a role="button" href="#"><a aria-current="false" href="/Help">HELP</a></a></li>
<li role="presentation" class=""><a role="button" href="#"><a aria-current="false" href="/logout">LOGOUT</a></a></li>
</ul></div></div></nav>
<div><h2>I am your Overlord! Fear me!</h2></div></div></div></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script rel="stylesheet" type="javascript" href="../node_modules/react-bootstrap/react-bootstrap.min.js"></script>
<script type="text/javascript" src="/static/js/bundle.js"></script>
</body>
</html>
index.css
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
App.css
.App {
text-align: center;
}
.App-logo {
animation: App-logo-spin infinite 20s linear;
height: 80px;
}
.App-header {
background-color: #222;
height: 150px;
padding: 20px;
color: white;
}
.App-title {
font-size: 1.5em;
}
.App-intro {
font-size: large;
}
@keyframes App-logo-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
渲染/输出CSS
element.style {
}
html {
font-size: 10px;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
html {
font-family: sans-serif;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html[Attributes Style] {
-webkit-locale: "en";
}
user agent stylesheet
html {
display: block;
}
:after, :before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
:after, :before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
在复制时,您可能需要手动更改isDev
,isAdmin
和/或isAnalyst
以使其中一个true
来帮助修改渲染
问题:
有没有人知道动态证明/居中Navbar项目的正确方法?
答案 0 :(得分:0)
从文档中可以找到Nav的一个名为justify,set的道具
<Nav className="justify-content-end nav" justify="true">
...
</Nav>
,然后将nav类设置为flex: 1;
,以便它占用Navbar中的所有空间。