【问题标题】:React Router: The Router only loads / path and does not load any other pathReact Router:Router只加载/路径,不加载任何其他路径
【发布时间】:2021-10-24 22:04:43
【问题描述】:

我正在尝试使用 Route 从 react-router-dom 加载路径,但它只获取“/”路径,它不加载任何其他路径,身份验证逻辑之前工作正常,但路由似乎没有正在工作,如果我将任何其他组件添加到“/”路径,它将开始加载,但如果我删除“/”并添加任何其他路径,例如“/auth”,它将显示一个空白的白色屏幕。我尝试了很多方法,但找不到任何解决方案, 在添加路由之前,我已经添加了正确的组件,它们工作顺利,现在我是初学者,这对你们来说可能很容易解决,请在这里帮助我,因为我被卡住了,找不到任何解决方案。我希望我的问题很清楚。

App.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();

【问题讨论】:

    标签: javascript reactjs react-router react-router-dom react-router-component


    【解决方案1】:

    使用“/”路由的确切路径。在您的代码中,路线包含“确切”关键字,但放错了位置

          let routes = (
            <Switch>
            <Route path='/auth' component={Auth} />
            <Route exact path= "/" component={Home}/>
            <Redirect to="/"/>
            </Switch>
           );
          
          if(authenticated){
    
            routes = (
              <Switch>
              <Route path= '/about' component={About}/>
              <Route path='/auth' component={Auth} />
              <Route exact path= '/' component={Home}/>
              <Redirect to='/'/>
              </Switch>
            )
    
          }
    
    

    【讨论】:

    • 属性的顺序无关紧要。 &lt;Route exact path="/"...&lt;Route path="/" exact... 相同。
    • 您发布的代码不是可运行的sn-p(只需单击“运行...”按钮,您就会意识到这一点)!请编辑您的答案并将您的代码格式化为普通代码块(使用简单的三个反引号代码围栏)。
    • 从来没有那样使用过“精确”属性。不知道。
    • 您好,我尝试了您的方法,但没有奏效,我想出了一个解决方案,请仔细查看,并告诉我它是否违反约定,是否会导致任何未来的错误?我将不胜感激,[链接] (imgur.com/VMlhmQb)
    • 在代码图片中可以看到我是替换root... "/" 认证前后的路由,以后会不会有什么bug
    【解决方案2】:

    我不确定您是否使用重定向。我不知道如何修复您的代码,但您为什么不尝试一下,也许它会有所帮助:

    import { BrowserRouter as Router, Route, Redirect } from 'react-router-dom';
    import ....
    .....
    const  App = props => {
    const authenticated = useContext(AuthContext).auth;
    return (
    <Router>
      <Route
         exact
         path="/auth"
         component={Auth}
      />
    
      <Route
         exact
         path="/"
         render={(props) =>(!authenticated ? <Redirect to={'/auth'}/> 
         : 
         <Home authenticated={authenticated} {...props} />)}
      />
       <Route
          exact
          path="/about"
          render={(props) => (!authenticated ? <Redirect to={'/auth'} 
          /> : 
          <About authenticated={authenticated} {...props} />)}
      />
     
    </Router> 
    )}
    

    【讨论】:

    • 您好,我尝试了您的方法,但没有奏效,我想出了一个解决方案,请仔细查看,并告诉我它是否违反约定,是否会导致任何未来的错误?我将不胜感激,[链接] (imgur.com/VMlhmQb)
    • 在代码图片中可以看到我是替换根... "/" 认证前后的路由,以后会不会引起什么bug
    • 所以我理解你的逻辑是:!authenticated => 只显示 (/auth) 路由。 Authenticated 为 true => 显示 Home、About 页面并隐藏 Auth 页面。如果这是您的目的,您的代码看起来不错。
    • 我在完全了解您的逻辑后编辑我的解决方案。我认为您的方法很好,但是如果您尝试我的方法,请告诉我它是否有效。谢谢!
    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2023-03-14
    • 2021-07-14
    • 2015-09-26
    • 2015-04-01
    • 2016-04-19
    相关资源
    最近更新 更多