import { Switch, Route, Router } from 'react-router';
import { Swtich, Route, BrowserRouter, HashHistory, Link } from 'react-router-dom';
1、api方面
React-router:
提供了路由的核心api。如Router、Route、Switch等,但沒(méi)有提供有關(guān)dom操作進(jìn)行路由跳轉(zhuǎn)的api;
React-router-dom:
提供了BrowserRouter、Route、Link等api,可以通過(guò)dom操作觸發(fā)事件控制路由。
Link組件,會(huì)渲染一個(gè)a標(biāo)簽;BrowserRouter和HashRouter組件,前者使用pushState和popState事件構(gòu)建路由,后者使用 hash 和 hashchange 事件構(gòu)建路由。
2、動(dòng)態(tài)路由跳轉(zhuǎn)
React-router:
router4.0以上?this.props.history.push('/path')?實(shí)現(xiàn)跳轉(zhuǎn);
router3.0以上?this.props.router.push('/path')?實(shí)現(xiàn)跳轉(zhuǎn);
React-router-dom:
直接用?this.props.history.push('/path')?實(shí)現(xiàn)跳轉(zhuǎn)
3、使用區(qū)別
react-router-dom在react-router的基礎(chǔ)上擴(kuò)展了可操作dom的api。
Swtich?和?Route?都是從react-router中導(dǎo)入了相應(yīng)的組件并重新導(dǎo)出,沒(méi)做什么特殊處理。
react-router-dom中package.json依賴中存在對(duì)react-router的依賴,故此,不需要npm安裝react-router。
* 可直接 npm 安裝 react-router-dom,使用其api。