我已经使用 BrowserRouter 创建了一个具有多个路由的 ReactJS 网页,并通过 GitHub 页面部署了它自己的域。该网站按预期完美运行,但是,当我在主页“/”页面以外的路线上刷新页面时,我收到来自 Github 的错误 404 错误。例如,我的域名是“kigaru-sushi.com/”。当我尝试刷新或输入网址“kigaru-sushi.com/sushi”时,我会进入此页面:
https://i.stack.imgur.com/VxgIU.png
当我在本地模拟时,它似乎工作正常。但是,当我运行脚本“npm run deploy”并在线查看它并刷新页面时,我似乎遇到了这个问题。
这是我的 package.json 的开头:
{
"name": "kigaru-app",
"version": "0.1.0",
"private": true,
"homepage": "https://kigaru-sushi.com/",
"dependencies": {
"@material-ui/core": "^4.3.0",
"@material-ui/styles": "^4.3.0",
"gh-pages": "^2.0.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-pose": "^4.0.8",
"react-redux": "^7.1.0",
"react-router-dom": "^5.0.1",
"react-scripts": "^2.1.8",
"redux": "^4.0.4"
}
...
和我在 App.js 中的路线:
render() {
return (
<BrowserRouter>
<div>
{this.state.isDesktop ? <Navbar /> : <Mobilenav openDrawer={this.state.openDrawer} closeDrawer={() => this.setState({openDrawer: false})}/>}
<Route exact path={process.env.PUBLIC_URL + '/'} render={ () => (<Home
handleListClick={() => this.setState({openDrawer: true})}
/>)} />
<Route
path="/sushi"
component={() => (
<Sushi
nigiri={sushi.nigiri}
gunkan={sushi.gunkan}
makirolls={sushi.makirolls}
desktop={this.state.isDesktop}
/>
)}
/>
<Route
path="/appetizers"
render={() => <Appetizers appetizers={appetizers} />}
/>
<Route
path="/maindish"
render={() => (
<Maindish
japaneseCurry={maindish.japaneseCurry}
noodles={maindish.noodles}
donburi={maindish.donburi}
/>
)}
/>
<Route
path="/drinks"
render={() => (
<Drinks
beer={drinks.beer}
chuHi={drinks.chuHi}
softDrinks={drinks.softDrinks}
dessert={drinks.dessert}
/>
)}
/>
<Route
path="/contact"
render={() => (
<Contact
/>
)}
/>
<Pop pose={this.state.showDialog ? "static" : "grow"}>
<Fab
onClick={this.handleClickButton}
style={{
position: "fixed",
bottom: "0",
right: "0",
zIndex: 2,
marginRight: "5px",
marginBottom: "10px"
}}
size={this.state.isDesktop ? "large" : "small"}
>
<Icon
style={
this.state.isDesktop
? { fontSize: "40px" }
: { fontSize: "30px" }
}
color={"error"}
>
favorite_border
</Icon>
</Fab>
</Pop>
<Shopping open={this.state.showDialog} close={this.handleClose} />
<Footer />
</div>
</BrowserRouter>
);
我试过删除 proccess.env.PUBLIC_URL 并添加一个基本名称,但都没有奏效,我非常迷茫。我也尽我最大的努力使用 HashRouter 但是当我部署它时,它似乎只给了我一个空白页面。任何帮助将不胜感激!