在本地,我可以很好地浏览到 /buttons 我什至可以刷新它,它也很好,但是当我将构建目录上传到 github 页面时,我无法访问 /buttons,而是获得了 GitHub 404 页面,而不是我自己的 'notfound ' 页。
如果我创建从主页到 /buttons 的链接,则按钮将加载,但直接浏览它不会加载。
import React, { useState } from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import { Context } from "./components/context";
import { Layout } from "./components/layout";
import { Home } from './routes/home';
import { Buttons } from './routes/buttons';
import { NotFound } from './routes/notfound';
const Router: React.FC = () => {
const [global, setGlobal] = useState({
language: localStorage.getItem("language") || 'en',
apiUrl: 'https://api.domain.com/api',
loggedIn: localStorage.getItem("jwt") ? true : false,
redirectUrl: '',
modal: false,
modalState: '',
});
return (
<Context.Provider value={{ global, setGlobal }}>
<BrowserRouter basename={process.env.PUBLIC_URL}>
<Route render = {({ location }) => (
<Layout location = { location }>
<Switch location = { location }>
<Route exact path = '/' component = { Home } />
<Route exact path = '/buttons/' component = { Buttons } />
<Route component = { NotFound }/>
</Switch>
</Layout>
)} />
</BrowserRouter>
</Context.Provider>
);
}
export { Router };
在 package.json 中,我确实将主页定义为:
"homepage": "https://myName.github.io/myRepo",