React Router V4 嵌套路由器在页面重新加载时不起作用
React Router V4 nested routers are not working on page reload
我用 React 创建了一个新网站 http://gk-chart.org/,并使用 react-rotuter-dom 添加了路由。
import {BrowserRouter as Router, Route, Switch} from "react-router-dom";
并将路由器添加为:-
<Switch>
<Route exact path="/" component={Home}></Route>
<Route path="/galery" component={Galery}></Route>
</Switch>
对于多种类型图表的嵌套路由,我在图库组件中添加了这些路由
<Route exact path={this.props.match.path} component={LineChart}></Route>
<Route path={`${this.props.match.path}/line`} component={LineChart}></Route>
<Route path={`${this.props.match.path}/lineChartFill`} component={LineChartFill}></Route>
<Route path={`${this.props.match.path}/lineChartComparision`} component={LineChartComparision}></Route>
<Route path={`${this.props.match.path}/lineChartComparisionFill`} component={LineChartComparisionFill}></Route>
路由器在点击链接时工作正常,但当我尝试重新加载页面时,页面没有再次加载。
在图库页面 http://gk-chart.org/galery 中存在多个嵌套路由,当我单击这些路由时它们会正确加载,但是当我重新加载页面时,它不会加载。
请帮我解决这个问题。
服务器不知道您的客户端路由。您的构建过程可能会生成一个引用您的资源的 index.html。 index.html 被放置在 /galery/index.html
。当您单击刷新时,服务器会在 /galery/line/index.html
查找文件,但它没有找到。
这是一个常见问题。为了解决这个问题,许多人告诉服务器服务于 /galery/index.html
而不是使用某种规则。这取决于您的文件的托管方式。 React 应用程序将在客户端启动,看到略有不同的路线,并正确呈现路线。
在为这个答案漫游了这么多网站之后,我终于知道 React 有两种类型的路由器 :-
用于静态路由网站的HashRouter,也可以称为自驱动路由。
用于服务器支持路由的 BrowserRouter。
我在我的网站上使用了错误的 BrowserRouter,因为我没有任何类型的服务器支持(节点、python 等...)
所以我必须使用 HashRouter
import {HashRouter as Router, Route, Switch} from "react-router-dom";
修复了 http://gkchart.com/ 使用 Hashrouter 的问题。
我用 React 创建了一个新网站 http://gk-chart.org/,并使用 react-rotuter-dom 添加了路由。
import {BrowserRouter as Router, Route, Switch} from "react-router-dom";
并将路由器添加为:-
<Switch>
<Route exact path="/" component={Home}></Route>
<Route path="/galery" component={Galery}></Route>
</Switch>
对于多种类型图表的嵌套路由,我在图库组件中添加了这些路由
<Route exact path={this.props.match.path} component={LineChart}></Route>
<Route path={`${this.props.match.path}/line`} component={LineChart}></Route>
<Route path={`${this.props.match.path}/lineChartFill`} component={LineChartFill}></Route>
<Route path={`${this.props.match.path}/lineChartComparision`} component={LineChartComparision}></Route>
<Route path={`${this.props.match.path}/lineChartComparisionFill`} component={LineChartComparisionFill}></Route>
路由器在点击链接时工作正常,但当我尝试重新加载页面时,页面没有再次加载。
在图库页面 http://gk-chart.org/galery 中存在多个嵌套路由,当我单击这些路由时它们会正确加载,但是当我重新加载页面时,它不会加载。 请帮我解决这个问题。
服务器不知道您的客户端路由。您的构建过程可能会生成一个引用您的资源的 index.html。 index.html 被放置在 /galery/index.html
。当您单击刷新时,服务器会在 /galery/line/index.html
查找文件,但它没有找到。
这是一个常见问题。为了解决这个问题,许多人告诉服务器服务于 /galery/index.html
而不是使用某种规则。这取决于您的文件的托管方式。 React 应用程序将在客户端启动,看到略有不同的路线,并正确呈现路线。
在为这个答案漫游了这么多网站之后,我终于知道 React 有两种类型的路由器 :-
用于静态路由网站的HashRouter,也可以称为自驱动路由。
用于服务器支持路由的 BrowserRouter。
我在我的网站上使用了错误的 BrowserRouter,因为我没有任何类型的服务器支持(节点、python 等...) 所以我必须使用 HashRouter
import {HashRouter as Router, Route, Switch} from "react-router-dom";
修复了 http://gkchart.com/ 使用 Hashrouter 的问题。