简体   繁体   English

反应路由器子路由导航栏不起作用

[英]React router sub routes navigation bar not working

I am trying to create a navigation bar with React.js and bootstrap, i have several Components and dont know how to make the links in the menu work, the problem is with subroutes, when i want to access a path like path='/book/booktwo' it just dont work, but simpler individual routes do work. 我正在尝试使用React.js和bootstrap创建一个导航栏,我有几个组件并且不知道如何使菜单中的链接正常工作,问题出在我想访问路径,例如path ='/ book / booktwo'只是行不通,但更简单的个人路线行得通。 Here is my code. 这是我的代码。 Thank you !!! 谢谢 !!!

    import React, { Component } from 'react';
import {IndexRoute, Route, browserHistory} from 'react-router';
import {Router} from 'react-router';
import Home from './components/Home';
import Books from './components/Books';
import Bookstopic from './components/Bookstopic';
import Booktwo from './components/Booktwo';
import Bookthree from './components/Bookthree';
import Navbars from './components/Navbars';

export default class Routes extends Component {
  render() {
    return (
      <Router history={browserHistory}>
        <Route exact path={'/'} component={Navbars} >
          <IndexRoute component={Home} />
            <Route path='{/books}' component={Books} >
              <IndexRoute component={Books} />
                  <Route path='{/book/:id}' component={Bookstopic} />
                  <Route path='{/book/booktwo}' component={Booktwo} />
                  <Route path='{/book/bookthree}' component={Bookthree} />
              </Route>           
        </Route>
      </Router>
    );
  }
}

import React, { Component, PropTypes } from 'react';

export default class Home extends Component {
  render() {
    return (
      <div>
        <h1>Hi Home !!! </h1>
      </div>
    );
  }
}

import React, { Component, PropTypes } from 'react';

export default class Booktwo extends Component {
  render() {
    return (
      <div>
        <h1>Hi Booktwo !!! </h1>
      </div>
    );
  }
}


import React, { Component, PropTypes } from 'react';
import Booktwo from './components/Booktwo';

export default class Bookstopic extends Component {
  constructor(props) {
    super(props);
  }
  //const bookid = this.props.params.bookid;
  render() {
    return <Booktwo />
  }
}


class Nav extends React.Component {
   render() {
    return (
     <div>  
      <ul id="navi">
        <li><a href="https://www.mybooks.com/">Home</a></li>
        <li><Link to={"/books"} activeStyle={{color: "red"}} activeClassName="hsubs" >Books</Link>
          <ul className="subs">
            <li><Link to={"/book/${booktwo}"} activeStyle={{color: "red"}}>Book Two</Link></li>
            <li><Link to={"/bookthree"} activeStyle={{color: "red"}}>Book Three</Link></li>
            </ul>
        </li>        
      </ul>
     </div> 
    );
  }
}



export default class Navbars extends React.Component {
  render() {
    return (
      <div className="contents">
        <Nav />
        <div className="row">
          <div className="col-xs-10 col-xs-offset-1">
            <Header />
          </div>
        </div>
        <div className="row">
          <div className="col-xs-10 col-xs-offset-1">
            {this.props.children}
          </div>
        </div>
      </div>
    );
  }
}

使用path={'/book/:id'}代替path='{/book/:id}'

I think there are two problems: 我认为有两个问题:

  1. Your quotation marks are outside of the curly braces. 您的引号不在花括号内。 Try writing it as path={'/books'} , etc. 尝试将其写为path={'/books'}等。

  2. You're not being specific enough with the route you're pointing the browser to. 您对浏览器所指向的路线的了解不够明确。 Your three subroutes are all matching on the first character, which might confuse it to think it is routing to '/' . 您的三个子路由在第一个字符上都匹配,这可能会使它误以为它正在路由到'/' I usually use exact path={/*string*/} to ensure the router doesn't get confused. 我通常使用exact path={/*string*/}来确保路由器不会混淆。 (though I'm not sure if that causes any problems as it hasn't for me as of yet) (尽管我不确定这是否会引起任何问题,因为到目前为止还不适合我)

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

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