简体   繁体   English

ReactJS Router无法导航到指定组件

[英]ReactJS Router not navigating to the specified component

I am pretty new to react-router. 我对React-router很陌生。 I created a single page with nav bars that can navigate to the either of two pages. 我创建了一个带有导航栏的页面,该导航栏可以导航到两个页面之一。 In one of the pages, I have a series of link that should direct to another component. 在其中一个页面中,我具有一系列链接,这些链接应直接指向另一个组件。 My app component deals with the initial routing 我的应用程序组件处理初始路由

            <Router>
                    <nav className ="nav">
                        <Link to='/' className="nav__nav-link-logo link"><img src={Logo} className="nav__nav-logo" alt="A figure that depicts school"></img></Link>
                        <button onClick={this.handleBarClick} className="nav__nav-bar-icon link"><FontAwesomeIcon icon={faBars} /></button>
                        <div className="nav__text-and-icon" style={{display : this.state.navTextDisplay}}>
                            <Link to = '/' className="nav__text link"><FontAwesomeIcon icon={faHome} className="icon" />  Home </Link>
                            <Link to = '/pages/Contact/Contact' className="nav__text link"><FontAwesomeIcon icon={faPhoneAlt} className="icon"/>  Contact the Dev</Link>
                        </div>
                    </nav>
                    <Switch>
                        <Route exact path='/' component = { Home } />
                        <Route exact path='/' component = { Home } />
                        <Route path='/pages/Contact' component = { Contact } />
                        <Route component={NotFoundPage}/>
                    </Switch>
           </Router>

then the home component renders a list of links I want to navigate to another component entirely. 然后home组件会呈现我要完全导航到另一个组件的链接列表。 PS : this is not about nest routing PS:这与嵌套路由无关

my list of links code is 我的链接代码列表是

      render() {
        return (
            <Fragment>
                {/**
                 * @param item the indiviual school generated from the API
                 */}
                {this.state.apiResponse.map((item, index) => {
                    return (
                        <Fragment>
                            <li key={item.schoollid}>
                                <Link
                                    to="/component/SchoolDetails/SchoolDetails"
                                    className="each-school"
                                >
                                    <SchoolTemplate item={item} />
                                </Link>
                            </li>
                            <Route
                                path="/component/SchoolDetails"
                                render={props => (
                                    <SchoolDetails {...props} details={item} />
                                )}
                                // component={SchoolDetails}
                            />
                        </Fragment>
                    );
                })}
            </Fragment>
        );

But then my routes is linking the individual links to my 404 (not found page) 但是,然后我的路线将各个链接链接到我的404(找不到页面)

try this. 尝试这个。

class APIRoutes extends React.Component {
 ...
 render() {
    return (
        <Switch>
            {this.state.apiResponse.map((item, index) => {
                return (
                    <Fragment>
                        ...
                        <Route
                            path="/component/SchoolDetails"
                            render={props => (
                                <SchoolDetails {...props} details={item} />
                            )}
                        />
                    </Fragment>
                );
            })}
        </Switch>
    );
}

And the component that have Router 和具有Router的组件

<Router>
   ...
<Switch>
    <APIRoutes />
    <Route exact path='/' component={Home}/>
    <Route path='/pages/Contact' component={Contact}/>
    <Route component={NotFoundPage}/>
</Switch>
</Router>

Try this it worked for me , 试试这个对我有用的

User Navbar React bootstrap plugin for the menu bar navigation , 用户Navbar React引导插件,用于菜单栏导航,

npm package: import {Navbar, Nav,NavItem, NavDropdown, MenuItem,Glyphicon, Label} from 'react-bootstrap'; npm软件包:从'react-bootstrap'导入{Navbar,Nav,NavItem,NavDropdown,MenuItem,Glyphicon,Label};

` `

</Navbar.Header>

<Navbar.Collapse>

    <Nav>
        <NavItem eventKey={1} href="#/Dashboard">
            Dashboard
        </NavItem>

        <NavDropdown eventKey={3} title="Tickets" id="basic-nav-dropdown">
            <MenuItem eventKey={3.1}>
                <NavLink exact to={ "/Home"}>Home</NavLink>
            </MenuItem>
            <MenuItem eventKey={3.2}>
                <NavLink exact to={ "/Gallary"}>Gallary</NavLink>
            </MenuItem>
    </Nav>
    </NavDropdown>
</Navbar.Collapse>

  <NavDropdown eventKey={3} title="Tickets" id="basic-nav-dropdown">
    <MenuItem eventKey={3.1} ><NavLink exact to={"/Home"} >Home</NavLink></MenuItem>
    <MenuItem eventKey={3.2}><NavLink exact to={"/Gallary"}>Gallary</NavLink></MenuItem>  
</Nav>

npm package:
import {Route, BrowserRouter,NavLink,HashRouter , Switch} from 'react-router-dom';

` `

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

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