vue新开页面需求

157 阅读1分钟

新开页面跳转(重写open函数)

window.open({
	path:'/member/info',
	query:{
			id:params.row.memberId
}}, '_blank');
// 实现
window.open = (function (openView) {
  return function (url, specs, replace) {
    if (arguments[1] == "_blank" && typeof arguments[0] === "object") {
      const { path, query, params } = arguments[0];
      const urlResolve = this.$router.resolve({
        path,
        query,
        params,
      });

      localStorage.setItem(window.location.origin + urlResolve.href, window.location.href);
      window.open(urlResolve.href, "_blank", "", false);
    }
    openView(url, specs, replace);
  };
})(window.open);

返回

// 使用
this.$router.go(-1)
// 实现
this.$router.go = (function (go) {
  return function (n) {
    if (window.history.length <= 1) {
      if (localStorage.getItem(window.location.href)) {
        window.open(localStorage.getItem(window.location.href), "_self");
      } else {
        this.$router.push({ path: "/" });
      }
      return false;
    }
    go(n);
  };
})(this.$router.go);