React router 2.4.0重定向错误行为

时间:2016-05-12 14:05:37

标签: reactjs react-router

有人可以给我一些帮助。

我是reactjs的新手,并尝试使用最新版本的react和react-router libs实现简单的应用程序。我在docs API中发现Redirect元素应该可以工作。

主要元素

var React = require("react");
var ReactDOM = require("react-dom");

var ReactRouter = require('react-router');
var Router = ReactRouter.Router;
var routes = require('./routes');
var browserHistory = ReactRouter.browserHistory;

ReactDOM.render(<Router history={browserHistory}>{routes}</Router>, document.getElementById("app"));

路线

var React = require('react');
var Router = require('react-router');
var IndexRoute = Router.IndexRoute;
var Route = Router.Route;
var PageNotFound = require('./components/NotFountpage');
var Redirect = Router.Redirect;

var routes  = (
    <Route path="/" component= {require('./components/app')} >
      <IndexRoute  component={ require('./components/homePage')} />
      <Route path="/about" component={require('./components/aboutPage')} />
      <Route path="*" component={PageNotFound} />
      <Redirect from="/azaza" to="/about"/>
      </Route>
    )

module.exports = routes;

我不知道是我的错误还是这个元素的库版本有问题。找不到页面也无法正常工作。似乎是我的错,我无法解决它。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

将重定向放在PageNotFound路由之前(假设情况更糟),并修改PageNotFound的路径,如下所示:

<Route path="/" component= {require('./components/app')} >
  <IndexRoute  component={ require('./components/homePage')} />
  <Route path="/about" component={require('./components/aboutPage')} />
  <Redirect from="/azaza" to="/about"/>
  <Route path="/*" component={PageNotFound} />
</Route>