React-Router中的父子路由

时间:2015-06-28 15:36:14

标签: javascript node.js reactjs react-router

我正在使用react-router在我的应用程序中的组件之间进行路由。如何在应用程序本身之外为登录页面创建路线?即,用户登陆页面并且LandingPage路由到应用程序,然后应用程序可以相应地路由导航栏中的组件/视图。

我遇到的问题是Navbar也会在着陆页中不断填充。

以下是路线:

// React
var React = require("react");
var Router = require("react-router");
var Route = Router.Route;
var NotFoundRoute = Router.NotFoundRoute;
var DefaultRoute = Router.DefaultRoute;

var App = require('./components/App.js');
var About = require('./components/About.js');
var Concat = require('./components/Concat.js');
var Home = require('./components/Home.js');
var Landing = require('./components/Landing.js');
var Login = require('./components/Login.js');
var Search = require('./components/Search');
var Signup = require('./components/Signup');

var routes = (
  <Route path="/" handler={App}>
    <DefaultRoute handler={Landing} />
    <Route name="about" handler={About}/>
    <Route name="concat" handler={Concat}/>
    <Route name="home" handler={Home}/>
    <Route name="login" handler={Login}/>
    <Route name="search" handler={Search}/> 
    <Route name="signup" handler={Signup}/>       
  </Route>
);

module.exports = routes;

这是App.js

var React  = require('react');
var Navbar = require('./Navbar.js');
var Router = require('react-router');
var Landing = require('./Landing.js');

var RouteHandler = Router.RouteHandler;

var App = React.createClass({
  render: function () {
    return (
      <div className="app-wrapper">
        <Navbar />
        <RouteHandler />
      </div>
    );
  }
});

module.exports = App;

这是Navbar:

var React  = require('react');
var Router = require('react-router');
var Link = Router.Link;

var Navbar = React.createClass({
  render: function () {
    return (
      <div className="navbar navbar-default navbar-static-top">
        <div className="container">
          <div className="navbar-header">
            <button type="button" className="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span className="icon-bar">test</span>
              <span className="icon-bar"></span>
              <span className="icon-bar"></span>
            </button>
          </div>
          <div className="collapse navbar-collapse">
            <ul className="nav navbar-nav">
              <li className="active"><Link to="home">Jobletics</Link></li>
              <li><Link to="concat">Create a Listing</Link></li>
              <li><Link to="search">Search Jobs</Link></li>
              <li><Link to="about">About</Link></li>
            </ul>
            <ul className="nav navbar-nav navbar-right">
              <li><Link to="login">Log in</Link></li>
              <li><Link to="signup">Sign up</Link></li>
            </ul>
          </div>
        </div>
      </div>
    );
  }
});

module.exports = Navbar;

任何想法?

2 个答案:

答案 0 :(得分:0)

在这种情况下,我会检查是否并查看当前路线是否为着陆路线,然后相应地隐藏导航栏。你也可以重构你的路线或你的组件中的html,但如果只有一条路线是例外我只会检查当前路线是否着陆。

答案 1 :(得分:0)

您可以尝试链接此..

  <BrowserRouter  >
                 <Route push={true} path="/users/activation/:atoken" component={Activation}/> 
  </BrowserRouter>