这是我第一次使用React和React Router,我遇到了一些深层链接问题 . 我已经构建了一个简单的SPA,在React Router的帮助下,我可以导航到mysite.com/work,mysite.com/about和mysite.com/work/:projectid . 如果我从站点的根目录开始(例如,mysite.com并单击“工作”),我可以导航到这些页面 . 我遇到的问题是深层链接(手动输入mysite.com/about或刷新mysite.com/about上的页面) . 这一切都可以在我的本地主机上运行,但不能在托管站点上运行(顺便说一下,它是在GitHub页面上托管的) .
这是因为我使用GitHub页面来托管我的网站吗?或者这是React Router的问题?
这是main.js的一部分:
var routes = (
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={Work} />
<Route path="/work" component={Work} />
<Route path="/work/:id" component={ProjectDetail} />
<Route path="/about" component={About} />
</Route>
<Route path="*" component={NotFound} />
</Router>
);
ReactDOM.render(routes, document.getElementById('main'));
1 回答
您可以从根遍历站点,因为路由全部由
react-router
处理,后者从根站点的js文件加载 ./about/index.html
中没有html文件 .gh-pages
托管静态网站,如果你没有html文件去mysite.com/about
,它可能会给你一个404 .如果您使用的是webpack,请尝试使用https://github.com/markdalgleish/static-site-generator-webpack-plugin .