zoukankan      html  css  js  c++  java
  • 管理react路由的history对象的插件history的使用介绍

    本文介绍如何使用history插件管理浏览记录

    1. history插件的使用

      history这个插件可以方便管理你的浏览记录
      cnpm install history --save
      import createHistory from 'history/createBrowserHistory'
      
    2. 三种方法

      有三种使用方式
          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 老版本浏览器使用,暂不介绍
      
    3. 基本使用功能

      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() // 监听解绑
      
    4. 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"
              } )
      
    5. 使用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方法类似
      
    6. 使用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(); 实现方法解绑
    1、路在何方? 路在脚下 2、何去何从? 每个人都在探索,未来的方向在何处。如果说某些方向是世人已经公认的,那么就先按照公认的去走吧(ps:站在巨人的肩膀上看世界会清晰)。 如果说方向,当今世人还不清晰准确。那么就大胆往前走吧,对与错并不重要。心中的方向更加重要。
  • 相关阅读:
    PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历
    # 数字签名&数字证书
    # Doing homework again(贪心)
    # Tallest Cows(差分)
    # ACM奇淫技巧
    # 二维前缀和+差分
    # 费解的开关(二进制+递推+思维)
    # log对数Hash映射优化
    # 起床困难综合症(二进制枚举+按位求贡献)
    # 最短Hamilton路径(二进制状态压缩)
  • 原文地址:https://www.cnblogs.com/yuanjili666/p/13740385.html
Copyright © 2011-2022 走看看