我使用BrowserRouter创建了一个带有多个路由的ReactJS网页,并通过GitHub Pages部署它,拥有自己的域名。该网站按预期完美运行,但是当我在除主页'/'之外的路由上刷新页面时,我从Github收到错误404错误。例如,我的域名为'kigaru-sushi.com/'。当我尝试刷新或输入网址'kigaru-sushi.com/sushi'时,我会进入这个页面:https://istack.dev59.com/VxgIU.webp。当我在本地进行模拟时,似乎一切正常。然而,在运行脚本“npm run deploy”并在线查看和刷新页面时,我似乎遇到了这个问题。
这是我的package.json开头:
这是我的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和添加basename,但都没有起作用,我很困惑。我也尽力使用HashRouter,但在部署时只能给我一个空白页面。非常感谢您的任何帮助!