【发布时间】:2017-10-03 00:57:40
【问题描述】:
我有我希望 React Router 处理的路径,并且我还有一个 express API 后端,我从 React 应用程序调用它来执行一些安全的 API 调用。
/#/ - 想要在这里提供应用程序/#/:id - 应用程序的唯一 URL,我使用 ID 从 React 调用某些快速端点。* - 所有快速 REST 端点
快递 app.js:
app.use(middleware);
app.use(require('webpack-hot-middleware')(compiler, {
log: console.log
}));
app.use('/api', [router_invokeBhApi]);
app.get('/#/*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
app.use(express.static(path.join(__dirname, '/dist')))
我的 React 路由组件:
export default (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
<Route path="/:id" component={ConsentForm} something="dope"/>
</Route>
);
这就是正在发生的事情:
- 转到 localhost:8000 使用 HomePage 组件为应用程序提供服务
- 转到 localhost:8000/#/ 也为应用程序提供 HomePage 组件
- 去localhost:8000/example 给我Cannot GET /example 这意味着快递正在工作
- 转到localhost:8000/api 给了我一个测试 JSON 对象,这是我从 express 发送的,这是正确的。
- 转到localhost:8000/#/somehashcode 仍然给我HomePage 组件,而它应该给我ConsentForm 组件。
我使用 React 开发工具检查了路由器组件:
- RouterContext 组件有一个名为routes 的对象,在routes[0] 内有一个childRoutes,它的路径为/:id。 routes[0] 也有一个路径 / 告诉我 React Router 正确加载了所有路由??
好困惑……
我在其中渲染整个应用程序的 React 文件:
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import Routes from './shared/components/Routes';
import './shared/base.css';
import './shared/customInput.css';
const ROOT_ELEMENT = 'app';
ReactDOM.render((
<Router history={browserHistory}>
{Routes}
</Router>
), document.getElementById(ROOT_ELEMENT));
【问题讨论】:
-
你能提供更多关于你正在使用的 react-router 版本的信息吗?根据他们最新的文档,您必须从他们的一个包中使用 HashRouter,而不仅仅是 Route。 reacttraining.com/react-router/web/api/HashRouter
-
@Michael "react": "^0.14.7", "react-bootstrap": "^0.28.3", "react-dom": "^0.14.7", "react-路由器”:“^2.0.0”,“react-router-dom”:“^4.1.1”,
-
App的渲染方法是什么样的?你有正确渲染子组件的地方吗? -
@gravityplanx 我已经用它更新了问题
-
仍然不包括我要求的信息。我专门询问名为
App的反应组件。它的渲染方法是否包含子组件的空间?
标签: reactjs express react-router