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

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

相关推荐
锈儿海老师几秒前
AST 工具大PK!Biome 的 GritQL 插件 vs. ast-grep,谁是你的菜?
前端·javascript·eslint
飞龙AI2 分钟前
鸿蒙Next实现瀑布流布局
前端
令狐寻欢2 分钟前
JavaScript中 的 Object.defineProperty 和 defineProperties
javascript
快起来别睡了3 分钟前
代理模式:送花风波
前端·javascript·架构
海底火旺5 分钟前
电影应用开发:从代码细节到用户体验优化
前端·css·html
陈随易14 分钟前
Gitea v1.24.0发布,自建github神器
前端·后端·程序员
前端付豪17 分钟前
汇丰银行技术架构揭秘:全球交易稳定背后的“微服务+容灾+零信任安全体系”
前端·后端·架构
邹荣乐20 分钟前
uni-app开发微信小程序的报错[渲染层错误]排查及解决
前端·微信小程序·uni-app
今天出摊吗20 分钟前
表单元素的默认提交行为
前端
今天出摊吗21 分钟前
CSS 动态视口单位 dvh 和 dvw
前端