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()
  }
})

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

相关推荐
拳打南山敬老院31 分钟前
Context 不是压缩出来的,而是设计出来的
前端·后端·aigc
用户30767528112735 分钟前
💡 从"傻等"到"流淌":我在AI项目中实现流式输出的血泪史(附真实代码+深度解析)
前端
bluceli36 分钟前
前端性能优化实战指南:让你的网页飞起来
前端·性能优化
UIUV36 分钟前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
SuperEugene38 分钟前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
没想好d39 分钟前
通用管理后台组件库-9-高级表格组件
前端
阿虎儿42 分钟前
React Hook 入门指南
前端·react.js
阿懂在掘金1 小时前
defineModel 是进步还是边界陷阱?双数据源组件的选择逻辑
vue.js·源码阅读
核以解忧1 小时前
借助VTable Skill实现10W+数据渲染
前端
WangHappy1 小时前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序