简体   繁体   English

如何在 JS 中使用 React Boostrap 创建 NavBar?

[英]How do I create a NavBar using React Boostrap in JS?

I am trying to create a boostrap react navbar, I am following a guide and this code SHOULD be working but it isn't.我正在尝试创建一个 boostrap 反应导航栏,我正在遵循一个指南,这个代码应该可以工作,但它不是。 Instead it only displays the "database" text and not the navbar.相反,它只显示“数据库”文本而不是导航栏。 Please do keep in mind that I know there are different ways of creating ne but I want to fix this code.请记住,我知道创建 ne 有不同的方法,但我想修复此代码。 Any help would be greatly appreciated!任何帮助将不胜感激!

App.js:应用程序.js:

import './App.css';

import {Home} from './Home';
import {Student} from './Student';
import {Professor} from './Professor';
import {Course} from './Course';
import {Navigation} from './Navigation';

import {BrowserRouter as Router, Routes, Route} from 'react-router-dom';

function App() {
  return (
    <Router>
      <div className="container">
        <h3 className="m-3 d-flex justify-content-center">
          Database
        </h3>

        <Navigation/>

        <Routes>
          <Route path='/' component={Home} />
          <Route path='/student' component={Student}/>
          <Route path='/professor' component={Professor}/>
          <Route path='/course' component={Course}/>
        </Routes>
      </div>
    </Router>
  );
}


export default App;

Navigation.js:导航.js:

import React,{Component} from 'react';
import {NavLink} from 'react-router-dom';
import {Navbar, Nav} from 'react-bootstrap';

export class Navigation extends Component
{
    render()
    {
        return
        {
            <Navbar bg="dark" expand="lg">
                <Navbar.Toggle aria-controls="basic-navbar-nav"/>
                <Navbar.Collapse id="basic-navbar-nav">
                    <Nav>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/">
                            Home
                        </NavLink>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/student">
                            Students
                        </NavLink>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/professor">
                            Professors
                        </NavLink>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/course">
                            Courses
                        </NavLink>
                    </Nav>
                </Navbar.Collapse>
            </Navbar>
        }
    }
}

return method inside class should have parenthesis instead of curly brackets.类内的返回方法应该有括号而不是大括号。

I fixed and added the answer.我修复并添加了答案。 Replace Navigation.js code by following.通过以下替换 Navigation.js 代码。

import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
import { Navbar, Nav } from 'react-bootstrap';

export class Navigation extends Component {
    render() {
        return (
            <Navbar bg="dark" expand="lg">
                <Navbar.Toggle aria-controls="basic-navbar-nav" />
                <Navbar.Collapse id="basic-navbar-nav">
                    <Nav>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/">
                            Home
                        </NavLink>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/student">
                            Students
                        </NavLink>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/professor">
                            Professors
                        </NavLink>
                        <NavLink className="d-inline p-2 bg-dark text-white" to="/course">
                            Courses
                        </NavLink>
                    </Nav>
                </Navbar.Collapse>
            </Navbar>
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM