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

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

相关推荐
青山Coding7 分钟前
Cesium应用(八):物体运动的实现思路
前端·cesium
用户41659673693558 分钟前
Android WebView 加载 file:// 离线页面调试教程
android·前端
Asmewill8 分钟前
curl命令学习笔记一
前端
我是一只快乐的小螃蟹8 分钟前
1.2 ArrayList 源码解析
前端
星栈9 分钟前
我用 Rust + Dioxus 做了个全栈跨平台笔记应用:再把新建、编辑和交付补上
前端·rust·前端框架
我是一只快乐的小螃蟹15 分钟前
1.1 HashMap (JDK1.8) 源码解析
前端
爱勇宝3 小时前
小红花成长新版:模板来了,鼓励也更容易开始
前端·后端·程序员
竹林8184 小时前
Solana前端开发:我在一个NFT铸造页面上被@solana/web3.js的Connection和Transaction签名坑了两天
前端
冬奇Lab4 小时前
每日一个开源项目(第144篇):ai-website-cloner-template - 一条命令、多 Agent 并行,把任意网站逆向成 Next.js 代码
前端·人工智能·开源
玄玄子4 小时前
webpack publicPath作用原理
前端·webpack·程序员