首页 > 解决方案 > 我的应用在调用登录时向 URL 添加了一个字符

问题描述

我正在使用前面的反应应用程序,每次我调用登录页面时,它都会在 url 中添加#

应用程序.js

...
import './App.scss'

const loading = () => <div className="animated fadeIn pt-3 text-center">Loading...</div>

const CVThequeIndex = React.lazy(() => import('./views/CVTheque/Index'))
const DepartIndex = React.lazy(() => import('./views/Depart/Index'))
const ServicesIndex = React.lazy(() => import('./views/Services/Index'))
const MonCvIndex = React.lazy(() => import('./views/MonCV/Index'))
const RecrutementIndex = React.lazy(() => import('./views/Recrutement/Index'))
const AccueilIndex = React.lazy(() => import('./views/Accueil/Index'))
const AdminIndex = React.lazy(() => import('./views/Admin/Index'))
const Login = React.lazy(() => import('./views/Pages/Login/Login'))
const PasswordRecover = React.lazy(() => import('./views/Pages/PasswordRecover/PasswordRecover'))
const Error404 = React.lazy(() => import('./views/Pages/Page404/Page404'))
const Error500 = React.lazy(() => import('./views/Pages/Page500/Page500'))

...


return (
  <HashRouter>
    <React.Suspense fallback={loading()}>
      <Switch>
        <CollaborateurRoute path="/cvtheque" name="CVTheque" render={props => <CVThequeIndex {...props} />} />
        <CollaborateurRoute path="/depart" name="Depart" render={props => <DepartIndex {...props} />} />
        <CollaborateurRoute path="/services" name="Services" render={props => <ServicesIndex {...props} />} />
        <CollaborateurRoute path="/moncv" name="MonCv" render={props => <MonCvIndex {...props} />} />
        <CollaborateurRoute path="/recrutement" name="Recrutement" render={props => <RecrutementIndex {...props} />} />
        <CollaborateurRoute path="/accueil" name="Home" render={props => <AccueilIndex {...props} />} />
        <AdminRoute path="/admin" name="Admin" render={props => <AdminIndex {...props} />} />
        <AuthenticatedRoute path="/deconnexion" name="Logout" render={props => <Login {...props} />} />
        <Route path="/connexion" name="Login" render={props => <Login {...props} />} />
        <Route exact path="/" name="Login" render={props => <Login {...props} />} />
        <Route exact path="" name="Login" render={props => <Login {...props} />} />

 ...

每次我尝试打开登录页面(或任何其他页面)时,都会在 URL 中添加一个 #。

在此处输入图像描述

标签: reactjsreact-router

解决方案


这是因为您使用的是HashRouter. 我不确定,但我假设你正在使用 React Router 的HashRouter. BrowserRouter如果您不想在 URL 中 使用井号,则需要使用 。https://reactrouter.com/web/api/BrowserRouter


推荐阅读