Vue界面跳转传递参数

界面跳转传递参数

1、push方式传参:

query传参 参数会在链接后面显示
      this.$router.push({
        path: "/home",
        query: { id: 1 },
      });

params传参 参数不会显示在链接后面
      this.$router.push({
        name: "home",
        params: { id: 1 },
      });

tis:

params传参使用name,query传参使用path

注意name的连接不要加上'/'

接收参数:

/this.$route.params

this.$route.query

2、window.href 传参:

window.location.href = '/home/?id='+1;

3、router-link标签跳转传参 :

  <router-link :to="{path:'home',query:{id:1}}">跳转</router-link>

4、利用resolve新打开新窗口传参:

  const {href}= this.$router.resolve({
                name:"animation",
                query:{
                    id:1
                }
  });
  window.open(href,"_blank") 

路由跳转不刷新

如果在使用Vue路由跳转页面后,页面没有更新,可能是因为缓存原因造成的

1、在路由的配置中引入meta,设置noCache为true:

const router = new VueRouter({
  routes: [
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: { noCache: true }
    }
  ]
})

2、在App.vue中加入代码:

<template>
  <div id="app">
    <router-view :key="$route.fullPath" />
  </div>
</template>

加入:key="$route.fullPath",当路由跳转时,会重新渲染组件。

3、在路由跳转时加上时间戳:

this.$router.push({
  path: '/home',
  query: {
    t: new Date().getTime()
  }
})

在跳转路由时,加上一个时间戳,可以让浏览器强制重新加载页面。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值