React 路由器仅适用于根路径 [英] React router only works with root path

查看:43
本文介绍了React 路由器仅适用于根路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有根路由有效./about,/home 在浏览器中显示无法获取/home".如果我用/"替换每个路由路径,那么相应的组件正在渲染.但是当我尝试渲染某些路线时似乎有问题,例如/home.

Only root route works. /about, /home shows "Cannot GET /home" in browser. If i replace each route path with "/" then the corresponding component is rendering. But there seems to be problem when i try to render certain route eg /home.

推荐答案

您必须配置您的服务器以使用 Router.HistoryLocation -- 也就是说,它需要始终为您的 index.html 提供服务页面与路线无关.

You have to configure your server to work with Router.HistoryLocation -- that is, it needs to always serve your index.html page regardless of the route.

app.get('*', function (req, res) {
  res.render('index');
});

在此处查看文档:React 路由器文档 - HistoryLocation

这篇关于React 路由器仅适用于根路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆