vue-cli单页面多入口的跳转及返回的一套解决方案

在由vue-cli构件的项目中,因为是单页面应用,经常导致手机多个地方有切入口的时候,做返回和跳转经常出现一些问题.

个人感觉可以整体对页面的浏览轨迹进行方法控制,不用借助原来的this.$router.go(-1)方法.

实现起来也很简单.就是做个公共数组变量来处理浏览的前进和后退.
historyrecords.js

var historyrecords = [];
  export default {
    historyrecords
  }

main.js引入

//定义一个总的历史记录管理数组
import historyrecords from "./publicjs/historyrecords";
Vue.prototype.$historyrecords = historyrecords;

然后到子页面进行处理

this.$historyrecords.historyrecords = common.handlehistoryrecords(
      this.$historyrecords.historyrecords
    );

公共方法里:

handlehistoryrecords: function (historyrecords) {
    if (!historyrecords || historyrecords.length == 0) {
      historyrecords = [
        {
          pageindex: 0,
          pageurl: window.location.href,
          pagehash:window.location.hash
        }
      ];
    } else {
      let lastobj = historyrecords[historyrecords.length - 1];
      let lasthashshort = lastobj.pagehash.split("?")[0];
      if (window.location.href.indexOf(lasthashshort)!=-1) { 
        return historyrecords;
      }
      if (historyrecords.length>=2) { 
        let prelastobj = historyrecords[historyrecords.length - 2];
        let prelastobjshort = prelastobj.pagehash.split("?")[0];
        if ( window.location.href.indexOf(prelastobjshort)!=-1) { 
          historyrecords.pop();
          return historyrecords;
        }
      }
      let getlength = historyrecords.length;
      historyrecords.push({
        pageindex: getlength,
        pageurl: window.location.href,
        pagehash:window.location.hash
      });
    }
    return historyrecords;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值