我正在使用react路由器dom创建react应用程序。
用yarn start
运行脚本从http://localhost:3000/(myprojectname)
开始,不是http://localhost:3000/
当使用react router dom进行路由时,我必须从url中删除myprojectname
,然后添加页面路由。
项目的初始设置似乎有问题
如何从http://localhost:3000/
开始??
添加package.json,路由器代码。
package.json:
{
"name": "cau-burger-online-order-system",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.3.2",
"@types/styled-components": "^5.1.16",
"axios": "^0.24.0",
"bootstrap": "^5.1.3",
"brace-expansion": "^2.0.1",
"gh-pages": "^3.2.3",
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "5.3.0",
"react-scripts": "4.0.3",
"styled-components": "^5.3.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"homepage": "https://myprojectname"
}
App.tsx:
import '../node_modules/bootstrap/dist/css/bootstrap.min.css'
import './App.css'
import { Route, BrowserRouter as Router, Switch } from 'react-router-dom'
import Detail from 'pages/detail/Detail'
import GlobalStyle from './GlobalStyle'
import Home from 'pages/home/Home'
import Login from 'pages/login'
import Signup from 'pages/signup'
function App() {
return (
<Router>
<GlobalStyle />
<div className="App">
<div className="auth-wrapper">
<Switch>
<Route exact path="/" component={Login} />
<Route path="/login" component={Login} />
<Route path="/signup" component={Signup} />
<Route path="/home" component={Home} />
<Route path="/detail" component={Detail} />
</Switch>
</div>
</div>
</Router>
)
}
export default App
在package.json
文件中,将homepage
条目更改为相对路径,不包括任何子域/嵌套目录。
{
"name": "cau-burger-online-order-system",
"version": "0.1.0",
"private": true,
"dependencies": {
....
},
"scripts": {
...
},
...,
"homepage": "https://hy57in.github.io/2021-Industry-Hands-On-Project"
}
至
{
"name": "cau-burger-online-order-system",
"version": "0.1.0",
"private": true,
"dependencies": {
...
},
"scripts": {
...
},
...,
"homepage": "./"
}
我在https://v5.reactrouter.com/web/api/Redirect"渲染将导航到新位置。新站点将像服务器端重定向(HTTP 3xx(那样覆盖历史堆栈中的当前位置;
有什么方法可以共享您的存储库吗?我将是帮助你的最佳方式。差不多,我需要更多的信息来帮助你。
您只需要做两件事就可以更改ReactRouterDom的基本Url。当你在像github页面这样的网站上或在像H这样的url上托管网站时,它很有用ttps://website.com/mywebsite或者在子链路上。
首先-你必须添加一个属性像-
<BrowserRouter basename='/mywebsite'>
或者打开你的package.json并添加Before building your project for production
"homepage": "/your-subdirectory",
对于我的案例:
"homepage": "/mywebsite",
我希望这会有所帮助,请随意询问你是否陷入了困境。