Create React App
npx create-react-app my-app
cd my-app
npm start
跨域
参考文档
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:5000',
changeOrigin: true,
})
);
};
React Router
react-router-dom
import React from 'react'
import { BrowserRouter, Route, Routes, Navigate } from 'react-router-dom';
import Login from '../views/login/Login';
import Home from '../views/home/Home';
export default function IndexRouter() {
function Auth() {
return localStorage.getItem('token') ? <Home /> : <Navigate to='/login' replace />
}
return (
<BrowserRouter>
<Routes>
<Route path='/login' element={<Login />} />
<Route path='/'
element={<Auth><Home /></Auth>} />
</Routes>
</BrowserRouter>
)
}