react-router与react-router-dom区别

import { Switch, Route, Router } from 'react-router';

import { Swtich, Route, BrowserRouter, HashHistory, Link } from 'react-router-dom';
复制代码

一、api方面

React-router:

提供了路由的核心api。如Router、Route、Switch等,但没有提供有关dom操做进行路由跳转的api;react

React-router-dom:

提供了BrowserRouter、Route、Link等api,能够经过dom操做触发事件控制路由。npm

Link组件,会渲染一个a标签;BrowserRouter和HashRouter组件,前者使用pushState和popState事件构建路由,后者使用 hash 和 hashchange 事件构建路由。json

二、动态路由跳转

React-router:

router4.0以上 this.props.history.push('/path') 实现跳转;api

router3.0以上 this.props.router.push('/path') 实现跳转;bash

React-router-dom:

直接用 this.props.history.push('/path') 实现跳转react-router

三、使用区别

react-router-dom在react-router的基础上扩展了可操做dom的api。dom

Swtich 和 Route 都是从react-router中导入了相应的组件并从新导出,没作什么特殊处理。this

react-router-dom中package.json依赖中存在对react-router的依赖,故此,不须要npm安装react-router。spa

  • 可直接 npm 安装 react-router-dom,使用其api。
相关文章
相关标签/搜索