我正在使用React-router,当我点击链接按钮时它工作正常,但是当我刷新我的网页时它没有加载我想要的东西.
例如,我进入localhost/joblist,一切都很好,因为我来到这里按一个链接.但是,如果我刷新网页,我得到:不能GET/joblist
默认情况下它没有像这样工作.最初我有我的URL:localhost /#/和localhost /#/ joblist,他们工作得非常好.但是我不喜欢这种网址,所以试图抹去我写的'#':
Cannot GET /joblist
Run Code Online (Sandbox Code Playgroud)
localhost /不会发生这个问题,这个问题总是返回我想要的.
编辑:这个应用程序是单页面,因此/ joblist不需要向任何服务器询问任何内容.
EDIT2:我的整个路由器.
Router.run(routes, Router.HistoryLocation, function (Handler) {
React.render(<Handler/>, document.body);
});
Run Code Online (Sandbox Code Playgroud) 我已经使用 Netlify 部署了我的站点,但在路由方面遇到了问题。
这是我的网站:https : //redux-co.netlify.com/
还有我的 GitHub 仓库:https : //github.com/jenna-m/redux-co
具体来说,如果用户导航到主页以外的任何页面并刷新页面,则默认 Netlify 404 呈现。从 404 页面,如果我导航回主页并刷新,则呈现主页。
此外,我的自定义 404 页面无法像我打开时那样工作localhost:3000
,但我想在处理我的自定义 404 组件之前先弄清楚这个刷新问题。
我正在使用 React 和 react-router,我知道由于我使用的是 react-router,我的网站不会立即部署。
这是我的_redirects
文件,它在/public
我的index.html
文件所在的文件夹中:
/* /index.html 200
Run Code Online (Sandbox Code Playgroud)
这是我“build”
位于package.json
:
…
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build && cp build/index.html build/404.html",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
…
Run Code Online (Sandbox Code Playgroud)
我读过其他人遇到过这种情况以及他们为克服这个问题所做的工作。这是我到目前为止所引用的……
https://www.freecodecamp.org/news/how-to-deploy-a-react-application-to-netlify-363b8a98a985/
https://hugogiraudel.com/2017/05/13/using-create-react-app-on-netlify/
这个人使用的是 Vue 而不是 React,但我还是尝试了这些解决方案:
https://github.com/vuejs/vuepress/issues/457#issuecomment-390206649
https://github.com/vuejs/vuepress/issues/457#issuecomment-463745656