我正在尝试使用 Route 从 react-router-dom 加载路径,但它只获取“/”路径,它不加载任何其他路径,身份验证逻辑之前工作正常,但路由似乎没有任何工作,如果我将任何其他组件添加到“/”路径,它将开始加载,但如果我删除“/”并添加任何其他路径,例如“/auth”,它将显示一个空白屏幕。我尝试了多种方法,但找不到任何解决方案,在添加路由之前,我已经添加了正确的组件,它们运行良好,现在我是初学者,这对你们来说可能很容易解决,请在这里帮助我因为我被卡住了广告找不到任何解决方案。我希望我的问题很清楚。
应用程序.js
import React, {useContext} from 'react';
import Auth from './Components/Auth/Auth';
import Home from './Pages/Home';
import { AuthContext } from './Context/auth-context';
import {Redirect, Route, Switch } from 'react-router-dom';
import About from './Pages/About';
const App = props => {
const authenticated = useContext(AuthContext).auth;
let routes = (
<Switch>
<Route path='/auth' component={Auth} />
<Route path= "/" exact component={Home}/>
<Redirect to="/"/>
</Switch>
);
if(authenticated){
routes = (
<Switch>
<Route path= '/about' component={About}/>
<Route path='/auth' component={Auth} />
<Route path= '/' exact component={Home}/>
<Redirect to='/'/>
</Switch>
)
}
return (
<div>
{routes}
</div>
);
}
export default App;
索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import ContextProvider from './Context/auth-context';
import {BrowserRouter} from 'react-router-dom';
ReactDOM.render(
<ContextProvider>
<BrowserRouter>
<App />
</BrowserRouter>
</ContextProvider>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.
reportWebVitals();