2016-09-29 66 views
2

我将我的react-router从版本0.13升级到2.8.1,然后给出错误提示“无法加载资源:服务器响应状态为404(未找到)“。从0.13升级到2.8.1后,React-router不起作用

这是我的代码。

import React from 'react' 
import Router from 'react-router' 

import App from './components/app'; 
import Main from './components/main'; 
import CreateOrganization from './components/create-organization'; 
import ManageUsers from './components/manage-users'; 
import FldList from './components/fld-list'; 
import FldView from './components/fld-view'; 
import WeighIn from './components/weigh-in'; 
import MatchFlds from './components/match-flds'; 
import NotFound from './components/not-found'; 

var { Route, DefaultRoute, NotFoundRoute } = Router; 

var routes = (
    <Route handler={App}> 
     <DefaultRoute handler={Main} /> 
     <Route name="CreateOrganization" path="create-organization" handler={CreateOrganization}></Route> 
     <Route name="manageUsers" path="manage-users" handler={ManageUsers}></Route> 
     <Route name="fldList" path="fld-list" handler={FldList}></Route> 
     <Route name="fldView" path="fld-view/:fldId" handler={FldView}></Route> 
     <Route name="weighIn" path="weigh-in" handler={WeighIn}></Route> 
     <Route name="matchFlds" path="match-flds" handler={MatchFlds}></Route> 
     <NotFoundRoute handler={NotFound} /> 
    </Route> 
); 

Router.run(routes, function (Handler) { 
    React.render(<Handler />, document.getElementById('react-container')); 
}); 

我应该在这里做什么来解决这个错误?

+1

翻阅新的API –

回答

2

API发生了某些变化,并且某些组件已被替换或被弃用。请看看API here并阅读upgrade guide here

,如果你做如下修改您的路线应该工作:

<Route path="/" component={App}> 
    <IndexRoute component={Main} /> 
    <Route name="CreateOrganization" path="create-organization" component={CreateOrganization}></Route> 
    <Route name="manageUsers" path="manage-users" component={ManageUsers}></Route> 
    <Route name="fldList" path="fld-list" component={FldList}></Route> 
    <Route name="fldView" path="fld-view/:fldId" component={FldView}></Route> 
    <Route name="weighIn" path="weigh-in" component={WeighIn}></Route> 
    <Route name="matchFlds" path="match-flds" component={MatchFlds}></Route> 
    <Route path="**" component={NotFound} /> 
</Route> 

您还需要替换此:

Router.run(routes, function (Handler) { 
    React.render(<Handler />, document.getElementById('react-container')); 
}); 

有:

import createBrowserHistory from 'history/lib/createBrowserHistory' 
let history = createBrowserHistory() 
render(<Router history={history}>{routes}</Router>, el) 

这里有一个不是全部)的简要清单电子:

  • handler属性现在被称为component
  • <DefaultRoute>组件现在被称为<IndexRoute>
  • <NotFoundRoute>组件现在已经过时。使用“全部”模式。 ***(非贪婪vs贪婪 - 阅读文档)。
5

这个答案已经上反应路由器进行了测试版本2.7.0

第2版的阵营路由器介绍IndexRoutebrowserHistory的概念。

如何配置您的路线的粗略概述如下。请查看官方文档,将其调整为您的使用案例。

import { Router, Route, IndexRoute, browserHistory } from 'react-router'; 

var routes = (
    <Router history={browserHistory}> 
     <Route path="/" component={App} /> 
     <IndexRoute component={Main} /> 
     <Route path="create-organization" component={CreateOrganization} /> 
     <Route path="manage-users" component={ManageUsers} /> 
     <Route path="fld-list" component={FldList} /> 
     <Route path="fld-view/:fldId" component={FldView} /> 
     <Route path="weigh-in" component={WeighIn} /> 
     <Route path="match-flds" component={MatchFlds} /> 
     <Route path="*" component={NotFound} /> 
     </Route> 
    </Router> 
); 

React.render(
    routes, 
    document.getElementById('react-container') 
); 

// Also, in newer versions of React you can use ReactDOM for mounting your app. 
// import ReactDOM from 'react-dom' 
//ReactDOM.render(
// routes, 
// document.getElementById('react-container') 
//); 
+0

感谢Piotr。那是4.0.0版本的风格吗?我认为它已经发生了很大的变化。我可能不得不通过升级来降级。 ;)必须更新我的代码,因为我们没有'react-container'我认为它是从项目文档复制粘贴的。我们实际上有Router.run(路由,React.render(,document.getElementById('content')); – user1567291

+1

嗨:)答案基于React Router 2.7.0规范,不幸的是它不再适用于React Router 4.0 0.0。我希望你找到一个好的解决方案。 –

+0

谢谢Piotr。我设法降级到路由器3.x并获得该部分的工作,而不是因为0.13 React代码库和所有更改而修复1000多个错误.React API和50多个第三方项目在两年内发生了变化,而不是向后兼容。 :D看起来像一个3个月的项目,让我们完成全面升级。所以必须在独立的分支上工作,同时完成所有其他新功能,升级并修复我们自己的应用代码库。好玩的东西。希望它全部向后兼容,以节省我们这个麻烦。 – user1567291

相关问题