如何在反应路由器中将子路由定义为反应组件
How to define child routes as react components in react-router?
我有main.js
:
import { render } from 'react-dom';
import { browserHistory, Router, Route, IndexRoute } from 'react-router';
import Home from './containers/Home';
import ApplicationsRoutes from './applications/routes';
render((
<Router history={browserHistory}>
<Route path="/" component="div">
<IndexRoute component={Home} />
<ApplicationsRoutes />
</Route>
</Router>
), document.getElementById('app'));
和/application/routes.js
:
import { browserHistory, Router, Route, IndexRoute } from 'react-router'
import ApplicationsHome from './containers/ApplicationsHome';
export default () => (
<Route path="applications" component={ApplicationsHome} />
);
我期望来自/application/routes.js
的路由将被添加到主路由器中,因此应用程序主页将被渲染为路径/applications
不幸的是,事实并非如此。我收到错误:
Location "/applications" did not match any routes
对我来说,看起来这种组成路由器的方式不起作用,但我不明白这样做的正确方法是什么。
通常,当您执行此操作时,您应该考虑使用 PlainRoute
语法而不是 JSX 语法,然后在父路由上使用 getChildRoutes
来解析子路由,根据 huge-apps 示例: https://github.com/rackt/react-router/tree/master/examples/huge-apps
然后,这将使您能够轻松配置代码拆分和动态路由。
对于单数路由,您可以将路由导出为 JSX 元素:
export default <Route path="applications" component={ApplicationsHome} />;
并像这样包含它:
{SomeRoute}
对于多个路由,您无法像在函数中那样导出它们,您将收到一个 JSX 错误,指出如果没有包装元素,您就不能拥有相邻的组件。在这种情况下,您必须执行以下操作:
export default (
<Route>
<Route path="/about" component={About} />
<Route path="/services" component={Services} />
</Route>
);
并使用它:
{ApplicationsRoutes}
相关文章:
- 尝试使用Node.js动态路由从IMDB中抓取电影内容.但是在我的output.json文件中没有定义
- 当我将 GET /any 发送到自定义路由“GET /:id”时,我得到 req.params.id = 'favico
- 从自定义指令调用角度路由
- angular-index.html只识别默认路由中定义的函数
- 使用可选参数和默认模型定义路由
- 自定义 Mod 文件夹的角度路由问题
- Angular JS TypeError:无法读取路由上未定义的属性“id”
- 使用路由的未定义服务控制器
- 在路由配置中定义 AngularJS 中的 ng-view 动画
- 部分而不是 app.js 中的角度路由定义
- rootScope 在更改路由时变得未定义
- Ember Data 1.0 加载路由时出错:类型错误:无法设置未定义的属性“类型键”
- 无法读取路由参数中未定义的属性“名称”
- Angular JS:我们可以在没有定义路由的控制器上设置解析或承诺吗?
- 自定义骨干路由参数
- 定义节点 js 的模式路由
- 如何在反应路由器中将子路由定义为反应组件
- 骨干路由器路由定义,以处理所有这些条件
- 404上的rails js调用,可能不正确的路由定义
- 在为rest路由定义视图时,数据是否会被传递给request .locals ?