反应路由器:路径='/'的路由不起作用

纳斯里德

我有这条路线:

const AppRouter = () => (
    <Router history={history}>
    <div>
        <Switch>
            <Route path='/' component={LoginPage} exact={true} />
        </Switch>
    </div>
    </Router>
);

它仅在我将 Route path 设置为空字符串时才有效''但是,日志history.location.pathname显示/它在 localhost:3000 上运行

我注意到这个有效:

<Route path=':3000/' component={LoginPage} exact={false} />
哈沙尔·乔杜里

您也不必在“localhost:3000”之后的浏览器窗口中包含“/”。

import React from 'react'  
import { BrowserRouter, Route, Switch } from "react-router-dom";
import ComponentName from './ComponentName';

const Router = () => {
<BrowserRouter>   
 <Switch>
          <Route exact path="/" component={ComponentName}/>
   </Switch>
</BrowserRouter>
}
export default Router;

之后将路由器组件导入 App.js。

import Router from './Router';
function App() {
  return <Router /> 
} 
export default App;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章