vue 跳转页面-$router.resolve和$router.push区别

打开方式

javascript 复制代码
router.push 只能当前窗口打开

router.resolve 结合 window.open 可以新窗口打开

参数传递

javascript 复制代码
router.push 支持query和params

router.resolve 只支持query,若需地址栏参数不可见,需结合localStorage或第三方插件保存

this.$router.push

javascript 复制代码
// 地址栏里带参
this.$router.push({
  path: '这里是path',
  query: {
    a: 1,
  },
});

// 地址栏里不带参
this.$router.push({
  name: '这里是name',
  params: {
    a: 1,
  },
});

注:

this.$ router.push用query传参对象时需注意的地方

用函数式编程this.$router.push跳转,用query传递一个对象时要把这个对象先转化为字符串,然后在接收的时候要转化为对象,要不然会接收不到参数。要不就把参数分开传递,不要放到对象里。

javascript 复制代码
this.$router.push({
    path: '/liveing',
    query: {
        routeParams: JSON.stringify({ liveUrl: url, flag: 2 })
    }
});

接收:

javascript 复制代码
let routeParams = JSON.parse(this.$route.query.routeParams)
this.currMeetingUrl = routeParams.liveUrl; 
this.obcject = routeParams.flag;

第二种方法:不要套在对象里直接传递

javascript 复制代码
this.$router.push({
    path: '/liveing',
    query: {
        liveUrl: url, 
        flag: 2
    }
});

接受:

javascript 复制代码
let liveUrl = this.$route.query.liveUrl;
let flag = this.$route.query.flag;

this.$router.resolve

javascript 复制代码
// 地址栏里带参
let data = this.$router.resolve({
  path: "/channel_sms",// 或者 name: 'channel_sms',
  query: {
    a: 1,
  },
});
window.open(data.href, '_blank');

// 地址栏里不带参
let data = this.$router.resolve({
  name: 'channel_sms',
});
localStorage.setItem('a', 1);
// 然后跳转页接收 localStorage.getItem('a');
相关推荐
子兮曰6 小时前
OpenClaw入门:从零开始搭建你的私有化AI助手
前端·架构·github
吴仰晖6 小时前
使用github copliot chat的源码学习之Chromium Compositor
前端
1024小神6 小时前
github发布pages的几种状态记录
前端
不像程序员的程序媛8 小时前
Nginx日志切分
服务器·前端·nginx
Daniel李华8 小时前
echarts使用案例
android·javascript·echarts
北原_春希8 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
JY-HPS8 小时前
echarts天气折线图
javascript·vue.js·echarts
尽意啊8 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜8 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive8 小时前
Vue3使用ECharts
前端·javascript·echarts