【问题标题】:React show routes if set up using react-router如果使用 react-router 设置,则反应显示路由
【发布时间】:2016-08-29 04:38:20
【问题描述】:

控制台上是否有我可以在运行时执行的命令来告诉我所有的路线?我使用过 react-router,但路由并非都有效。在 Rails 中,您可以在运行时获取列表。

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

您可以使用以下库在数组中获取routes

https://github.com/alansouzati/react-router-to-array

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import reactRouterToArray from 'react-router-to-array';
// or var reactRouterToArray = require('react-router-to-array');

console.log(reactRouterToArray(
<Route path="/" component={FakeComponent}>
{/* just to test comments */}
<IndexRoute component={FakeComponent} />
<Route path="about" component={FakeComponent}>
  <Route path="home" component={FakeComponent} />
  <Route path="/home/:userId" component={FakeComponent} />
</Route>
<Route path="users" component={FakeComponent} />
<Route path="*" component={FakeComponent} />
</Route>)
); //outputs: ['/', '/about', '/about/home', '/users']

【讨论】:

  • 如果此答案对您有用,您可以将答案标记为正确答案:)
猜你喜欢
  • 2016-07-09
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 2016-11-12
  • 2017-07-14
  • 2016-11-14
相关资源
最近更新 更多