Route
组件还可以嵌套。
<Router history={hashHistory}> <Route path="/" component={App}> <Route path="/repos" component={Repos}/> <Route path="/about" component={About}/> </Route> </Router>
上面代码中,用户访问/repos
时,会先加载App
组件,然后在它的内部再加载Repos
组件。
App
组件要写成下面的样子。
export default React.createClass({ render() { return <div> {this.props.children} </div> } })
上面代码中,App
组件的this.props.children
属性就是子组件。
子路由也可以不写在Router
组件里面,单独传入Router
组件的routes
属性。
let routes =
;
三、 path 属性
Route
组件的path
属性指定路由的匹配规则。这个属性是可以省略的,这样的话,不管路径是否匹配,总是会加载指定组件。
请看下面的例子。
上面代码中,当用户访问/inbox/messages/:id
时,会加载下面的组件。
如果省略外层Route
的path
参数,写成下面的样子。
现在用户访问/inbox/messages/:id
时,组件加载还是原来的样子。
四、通配符
path
属性可以使用通配符。
// 匹配 /hello/michael
// 匹配 /hello/ryan
// 匹配 /hello
// 匹配 /hello/michael
// 匹配 /hello/ryan
// 匹配 /files/hello.jpg
// 匹配 /files/hello.html
// 匹配 /files/
<