管理react路由的history对象的插件history的使用介绍
本文介绍如何使用history插件管理浏览记录
history插件的使用
history这个插件可以方便管理你的浏览记录
cnpm install history --save
import createHistory from 'history/createBrowserHistory'
三种方法
有三种使用方式
createBrowserHistory 现代浏览器使用
createBrowserHistory({
basename: '', // 基链接
forceRefresh: false, // 是否强制刷新整个页面
keyLength: 6, // location.key的长度
getUserConfirmation: (message,callback) => callback(window.confirm(message)) // 跳转拦截函数
})
createMemoryHistory 手机端使用
createMemoryHistory({
initialEntries: ['/'], // 初始载入路径,和MemoryRouter中的initialEntries是一样的
initialIndex: 0, // initialEntries初始载入索引
keyLength: 6, // location.key的长度
getUserConfirmation: null // 路由跳转拦截函数
})
createHashHistory 老版本浏览器使用,暂不介绍
基本使用功能
const history = createHistory(); 创建历史对象
const location = history.location; 获取location对象
const unlisten = history.listen( (location, action) => {
console.log(location,action)
// location是location对象
// action是动作名称,比如 "PUSH"
} )
history.push('/a', { some: 'state' }) // 强制跳转
unlisten() // 监听解绑
history对象
属性:
上面三种方法创建的history对象都有如下三个属性
history.length 历史记录的条数
history.location 历史记录中的location对象
history.action 当前的历史记录是通过什么动作添加进来的,如 "PUSH"
createMemoryHistory中提供了额外的两个属性
history.index 当前历史记录的索引
history.entries 历史记录
方法
listen方法
history.listen( (location,action) => {
console.log(location,action);
// location就是window.location的一个子集
// action可能的值,"PUSH", "REPLACE", "POP"
} )
使用history实现跳转
push
使用push可以将一条新的历史记录推送到历史堆栈中
history.push('/a');
history.push('/a',{name: 'yejiawei'});
history.push({
pathname: '/a',
state: {
name: 'yejiawei'
}
});
replace方法和push方法使用形式一样,replace的作用是取代当前历史记录
go,此方法用来前进或者倒退,history.go(-1);
goBack,此方法用来回退,history.goBack();
goForward,此方法用来前进,history.goForward();
另外使用createMemoryHistory创建的history对象,有canGo方法,和go方法类似
使用history实现路由跳转警告
const unblock = history.block('Do you want to leave?');
上面这个用法,就是添加一个跳转提示信息,默认使用dom环境的window.confirm,所以如果使用非dom环境的createMemoryHistory就要使用getUserConfirmation方法实现
另外,除了传递一个字符串提示信息以外,还可以添加函数
const unblock = history.block( (location,action) => {
return 'do you leave?'
} )
可以通过直接调用,unblock(); 实现方法解绑
管理react路由的history对象的插件history的使用介绍的更多相关文章
- 《JAVASCRIPT高级程序设计》window/location/navigator/screen/history对象
如果要在web中使用JAVASCRIPT,那么BOM(浏览器对象模型)毫无疑问是最重要的部分.BOM提供了很多对象,例如,window.location.navigator.screen.histor ...
- javascript深入之location对象和history对象
浏览器的location 和history对象: 一.location对象: 1>location.reload() 相当于按浏览器上的“刷新”(IE)或“Reload”(Netscape)键. ...
- javascript History对象属性和方法
History对象 History对象包含用户(在浏览器窗口中)访问过的URL length: 返回浏览器历史列表中的URL数量(打开浏览器,访问淘宝,返回1,再访问百度,返回2) History对象 ...
- 浅谈history对象以及路由插件原理
简介 History对象最初设计用来表示窗口的浏览历史,但是,出于隐私方面的原因,History对象不再允许脚本访问已经访问过的实际URL.虽然,我们不清楚历史URL,但是,我们可以通过History ...
- 深入学习 History 对象管理浏览器会话历史
History对象允许我们操作浏览器会话历史,即加载当前页面的标签页窗口或frame窗口的访问历史.之前有同学咨询我如何实现拦截用户跳转页面并强制用户返回首页后重新请求页面,于是有了本篇博客的主题,本 ...
- 七天接手react项目 系列 —— react 路由
其他章节请看: 七天接手react项目 系列 react 路由 本篇首先讲解路由原理,接着以一个基础路由示例为起点讲述路由最基础的知识,然后讲解嵌套路由.路由传参,最后讲解路由组件和一般组件的区别,以 ...
- react 路由使用react-router-dom
react 和vue一样都是使用封装history 来进行页面跳转,下面就来说一下react常用的路由插件react-router-dom这个东西在GitHub上 目前是最受欢迎的 首相还是先下载 n ...
- vue 和 react 路由跳转和传参
react 1 .跳转方式加传参 this.props.history.push({ //地址 pathname: '/film/Details', //路由传参 ...
- react 路由 react-router@3.2.1
react路由,4.x的差异还是比较大,暂时还是3.x的版本 安装: npm install -S react-router@3.x 配置: import { Router, Route, hashH ...
随机推荐
- [转载]Spring配置文件详解一:
原文地址:与base-package="com.xx">Spring配置文件详解一:<context:annotation-config/>与<contex ...
- myeclipse下搭建hadoop2.7.3开发环境
需要下载的文件:链接:http://pan.baidu.com/s/1i5yRyuh 密码:ms91 一 下载并编译 hadoop-eclipse-plugin-2.7.3.jar 二 将had ...
- <转载>Win x86-64 - Download & execute (Generator)
#Title: Obfuscated Shellcode Windows x86/x64 Download And Execute [Use PowerShell] - Generator #leng ...
- vc 改变控制台字符颜色
#include <Windows.h> #include <stdio.h> #include <iostream> using namespace std; i ...
- Qt之密码框不可全选、复制、粘贴无右键菜单等
转载---> http://blog.sina.com.cn/s/blog_a6fb6cc90101artk.html 在做用户登录界面的时候,往往会用到密码框,则其中的一些功能也要求与普通的输 ...
- 自己动手写OpenStack的QoS功能(4)
本文地址:http://blog.csdn.net/spch2008/article/details/9283561 数据库相应操作已完成,对OVS-Plugin进行修改. 在quantum\plug ...
- js 图片加载失败处理方法
在项目中不可避免会用到图片,尤其是列表,有时候图片会加载失败:这样就会显示一个很难看的坏图片缩略图:下面介绍两种方法,解决这个问题: 1.如果在你的项目中有引入jQuery插件,你可以使用error( ...
- gbk编码汉字转换成对应的十进制十六进制的值
http://www.mytju.com/classcode/tools/urlencode_gb2312.asp
- 为WPF和Silverlight的Grid添加边框线
http://www.cnblogs.com/chenxizhang/archive/2011/09/22/2185414.html
- 高性能Js—数据存取
数据存取 JavaScript中四中基本的数据存取位置 字面量:不存于某个变量内 本地变量:var定义的 数组元素 对象成员 字面量.全局变量读取速度 > 数组项.对象成员 .因为局部变量存在于 ...