[英]Externalizing route paths using react-router
I have a simple React Router Handler: 我有一个简单的React Router Handler:
var routes = (
<Route name="main" path="/" handler={require('./main')}>
<Route name="page1" path="/page1" handler={require('./page1')}/>
<Route name="page2" path="/page2" handler={require('./page2')}/>
<Route name="page3" path="/page3" handler={require('./page3')}/>
<DefaultRoute handler={require('./signin')}/>
</Route>
);
I'd like to externalize the page1, page2, page3 properties in a separate file (say a json file) and iterate that within the routes. 我想在单独的文件(例如json文件)中外部化page1,page2,page3属性,并在路由中进行迭代。 Something like this:
像这样:
var routes = (
<Route name="main" path="/" handler={require('./main')}>
_.each (routes, function(r) {
<Route name={r.name} path={r.path} handler={require(r.module)}/>
});
<DefaultRoute handler={require('./main')}/>
</Route>
);
Obviously I cannot have such a construct within routes, what is the React way of achieving this? 显然,我不能在路由中使用这样的构造,React的实现方式是什么?
Or is there a way to alter the routes via an api? 或者有没有办法通过api更改路线?
For eg: 例如:
routes.addRoute({name: r.name, path: r.path, handler: r.module}) ?
react-router just added top-level API for defining routes without JSX in version 0.12.2: react-router刚刚在0.12.2版中添加了用于定义不带JSX的路由的顶级API:
Router.createRoute()
Router.createDefaultRoute()
Router.createNotFoundRoute()
Router.createRedirect()
The doc annotations in the Route.js source and the detailed commit note are probably the best places to look while you wait until official docs and examples appear. 在等到正式文档和示例出现时, Route.js源代码中的doc注释和详细的提交说明可能是最佳的查找方式。
I think you should be able to do it with jsx
, but there is an error in your jsx
syntax. 我认为您应该可以使用
jsx
做到这jsx
,但是jsx
语法中有错误。 Try this: 尝试这个:
var routes = (
<Route name="main" path="/" handler={require('./main')}>
{
_.each (routes, function(r) {
return <Route name={r.name} path={r.path} handler={require(r.module)}/>;
});
}
<DefaultRoute handler={require('./main')}/>
</Route>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.