2016-07-02 45 views
1

是否有可能通过反应路线来处理以下内容?反应路线将子路线移动到单独的模块?

主要

<Routes handler={App}> 
    <Profile path="profile" /> 
    <Explore path="explore" /> 
    </Routes> 

档案

<Route> 
     <Route name="dashboard" handler={ProfileDashboard} /> 
     <Route name="repos" handler={ProfileRepos} /> 
    </Route> 

探索

<Route> 
     <Route name="home" handler={ExploreHome} /> 
     <Route name="showcase" handler={ExploreShowcase} /> 
    </Route> 
+0

我的问题是你为什么要这么做? –

回答

1

可以实现与阵营路由器! :)

但我建议你检查出来的“普通路由”的方式来配置你的路线:

https://github.com/reactjs/react-router/blob/master/docs/guides/RouteConfiguration.md#configuration-with-plain-routes

利用这一点,你就会开始一个routes对象的工作,你可以只需require其他路线并根据这些组合创建您的路线。类似的东西:

const routes = { 
    path: '/', 
    component: App, 
    childRoutes: [ 
     require('./profile'), 
     require('./explore') 
    ] 
} 

然后在你profile.js(你可以在同一做到explore.js)文件,你将有类似的东西:

/* Import the ProfileDashboard and ProfileRepos here */ 

const profileRoutes = { 
    path: 'profile', 
    childRoutes: [{ 
     path: 'dashboard', 
     component: ProfileDashboard 
    }, { 
     path: 'repos', 
     component: ProfileRepos 
    }] 
}; 

而且这种方式可以实现你想要的。

如果你真的不能使用普通的路线,你可以做这样的事情:

<Route path="/" component={App}> 
    { require('./profile') } 
    { require('./explore') } 
</Route> 

而你profile.js,例如:

module.exports = (
    <Route path="profile"> 
     <Route path="dashboard" component={ProfileDashboard} /> 
     <Route path="dashboard" component={ProfileRepos} /> 
    </Route> 
); 

我不知道是什么阵营路由器版本,但您可以在任何版本中实现该版本,但作为建议,请尝试使用最新的版本。因为它处理很多很酷的东西。

希望它有帮助!

+0

你自己试过这个吗?我尝试过这样的东西,但它不起作用! –

+0

是的,它工作。你使用什么反应/反应路由器版本? –

+0

是的,只是测试了出来感谢https://github.com/alikor/spike-react-routes –