vue-router路由跳转与打开新窗口

vue-router打开新窗口的方法及跳转方式对比

打开新窗口

方法1:

      const routeUrl = this.$router.resolve({

        path: "/targetUrl",

        query: { id: 96 },

      });

      window.open(routeUrl.href, "_blank");

    },

方法2:

    <router-link target="_blank" :to="{ path: '/catalog', query: { id: '1' } }"

      >打开新的标签页</router-link>

如果通过iframe嵌入到其他系统中,这样打开新窗口,会丢掉iframe的壳子,出现有问题

需改为window.parent.open(routeUrl.href, “_blank”);

注意同源问题,需要解决

vue-router的几种跳转方式

this.$router.push()

push向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL

首页 ===》通过直接修改地址来到页面A ===》通过点击按钮,this.$router.push来到页面B

那么这个时候点击浏览器的返回,首先返回到页面A,再次点击返回才返回到首页

this.$router.replace()

replace不会向 history 添加新记录,而是用心的替换掉当前的 history 记录

首页 ===》 通过直接修改地址来到页面A ===》通过点击按钮,this.$router.replace来到页面B

此时点击浏览器的返回,那么会直接回到首页

router.push({ path: '/home', replace: true })
this.$router.replace('/home')

上面这两种方法是等效的

router-link标签

<router-link to="/url"></router-link>

等同于调用
router.push(“/url”)

this.$router.go()

前进或后退,可接收一个数字

this. r o u t e r . g o ( − 1 ) : 后 退 ; t h i s . router.go(-1):后退; this. router.go(1)退this.router.go(0):刷新;
this.$router.go(1) :前进

如果 history 记录不够用,那就默默地失败: this.$router.go(1000)

this.$router.back()

后退 ;

this.$router.forward()

前进

this.$router.resolve+window.open打开新窗口

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值