react-router5.x 的配置及其頁面跳轉方法和js跳轉方法


https://blog.csdn.net/sinat_37255207/article/details/90745207

上次用react-router 的時候  還是3.x 很久不用 已經到react-router5.x 了  

廢話不多說 上代碼

配置

react: ^16.8.6,

react-dom: ^16.8.6,

react-router: ^5.0.0,

react-router-dom: ^5.0.0,

 

import {HashRouter as Router,Route,Link,BrowserRouter  ,Switch,Redirect} from 'react-router-dom';
 
....
<Router>
          <Link to="/a" style={{color:'black'}}>
              <div>點擊跳轉到a</div>
              </Link>
              <Link to="/b" style={{color:'black'}}>
              <div>點擊跳轉到b</div>
              </Link>
            
                <Route exact path='/' component={App1}/>
                
                <Route path='/a' component={App1}/>
                <Route path='/b' component={App2}/>
            
        </Router>

js 跳轉方法

import { createHashHistory,createBrowserHistory } from 'history'; // 是hash路由 history路由 自己根據需求來定
 
const history = createHashHistory();
 
...
 history.push('/a');
...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM