【发布时间】:2015-11-28 00:08:13
【问题描述】:
我正在开发 React-Express-Node 应用程序并专注于 SPA。我正在使用 react-router。
我的 server.js 文件如下所示(仅路由部分):
app.use(function(req, res, next) {
Router.run(routes,function(Handler, state) {
var ele = React.createElement(Handler);
res.render(path.join(__dirname + '/public/index'), {html: html});
});
next();
});
并且路由文件有这个代码(粘贴主要部分):
module.exports = (
<Route name="app" path="/" handler={Main}>
<Route name="about" path="about" handler={About}/>
<Route name="about/id" path="about/:id" handler={About}/>
<DefaultRoute name="default" handler={Home} />
</Route>
);
client.js 看起来像这样:
Router.run(routes, function(Root,state){
React.render(<Root />,document.getElementById('app'));
});
此设置运行良好,没有任何问题。
现在,我想使用 History API pushstate,以便我可以拥有更好的 url 并摆脱 #.为此,我在 client.js 中添加了 Router.HistoryLocation 作为第二个参数,它可以工作,它删除了 # 并提供了干净的 url。但是,我的页面刷新了我不想要的。
我对此进行了全面搜索,找到了几个解决方案,但它们要么使用 Flux,要么使用自定义路由器。我肯定错过了一些与状态有关但无法弄清楚的东西。有人能指出我正确的方向吗?
【问题讨论】:
标签: node.js express reactjs react-router