Vue界面跳转传递参数

界面跳转传递参数

1、push方式传参:

javascript 复制代码
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 传参:

javascript 复制代码
window.location.href = '/home/?id='+1;

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

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

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

javascript 复制代码
  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()
  }
})

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

相关推荐
用户21411832636029 分钟前
dify案例分享-5分钟搭建智能思维导图系统!Dify + MCP工具实战教程
前端
augenstern41612 分钟前
HTML(面试)
前端
excel18 分钟前
前端常见布局误区:1fr 为什么撑爆了我的容器?
前端
烛阴22 分钟前
TypeScript 类型魔法:像遍历对象一样改造你的类型
前端·javascript·typescript
vayy28 分钟前
uniapp中 ios端 scroll-view 组件内部子元素z-index失效问题
前端·ios·微信小程序·uni-app
专注API从业者1 小时前
基于 Node.js 的淘宝 API 接口开发:快速构建异步数据采集服务
大数据·前端·数据库·数据挖掘·node.js
前端无冕之王1 小时前
一份兼容多端的HTML邮件模板实践与详解
前端·css·数据库·html
啃火龙果的兔子1 小时前
js获取html元素并设置高度为100vh-键盘高度
javascript·html·计算机外设
再学一点就睡2 小时前
深入理解 Redux:从手写核心到现代实践(附 RTK 衔接)
前端·redux
天天进步20153 小时前
从零到一:现代化充电桩App的React前端参考
前端·react.js·前端框架