iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >this.$router.push携带参数跳转页面的实现代码
  • 208
分享到

this.$router.push携带参数跳转页面的实现代码

this.$router.push跳转页面this.$router.push带参数跳转页面 2023-05-14 06:05:50 208人浏览 独家记忆
摘要

目录前言一、params和query使用方式二、实现代码1.index.js代码2.test.Vue代码3.testParams代码4.testParams代码5.效果总结前言 th

前言

this.$router.push进行页面跳转时。携带参数有params和query两种方式。

一、params和query使用方式

query方式:
this. r o u t e r . p u s h ( p a t h : ′ t e s t Q u e r y ′ , q u e r y : t e s t Q u e r y : ′ t e s t Q u e r y ′ ) , 传 递 的 参 数 会 拼 接 在 跳 转 地 址 的 后 面 。 使 用 t h i s . router.push({path: 'testQuery',query: {testQuery:'testQuery'}}),传递的参数会拼接在跳转地址的后面。使用this. router.push(path:′testQuery′,query:testQuery:′testQuery′),传递的参数会拼接在跳转地址的后面。使用this.route.params.key取值
params方式:
this. r o u t e r . p u s h ( n a m e : ′ t e s t P a r a m s ′ , p a r a m s : t e s t P a r a m s : ′ t e s t P a r a m s ′ ) , 传 递 的 参 数 不 会 拼 接 在 跳 转 的 后 面 。 使 用 t h i s . router.push({name: 'testParams',params: {testParams:'testParams'}}),传递的参数不会拼接在跳转的后面。使用this. router.push(name:′testParams′,params:testParams:′testParams′),传递的参数不会拼接在跳转的后面。使用this.route.query.key取值

二、实现代码

1.index.js代码

const router = new Router({
    routes: [
        {
            path:'/test',
            component: test,
        },
        {
            name: 'testParams',
            path:'/testParams',
            component: TestParams,
        },
        {
            path:'/testQuery',
            component: TestQuery,
        }
    ]
})

2.test.vue代码

<template>
  <div class="test">
    <button v-on:click="testParams">params</button>
    <button v-on:click="testQuery">query</button>
  </div>
</template>
<script>
export default {
  name: "test",
  data(){
    return {

    }
  },
  methods:{
    testParams(){
      this.$router.push({name: 'testParams',params: {testParams:'testParams'}});
    },
    testQuery(){
      this.$router.push({path: 'testQuery',query: {testQuery:'testQuery'}});
    }
  }
}
</script>

3.testParams代码

<template>
  <div id="testParams">
    {{testParams}}
  </div>
</template>

<script>
export default {
  name: "TestParams",
  data() {
    return{
      testParams: ''
    }
  },mounted() {
    this.testParams = this.$route.params.testParams;
  }
}
</script>

4.testParams代码

<template>
  <div id="testQuery">
    {{testQuery}}
  </div>
</template>

<script>
export default {
  name: "TestQuery",
  data(){
    return{
      testQuery: ''
    }
  },mounted() {
    this.testQuery = this.$route.query.testQuery;
  }
}
</script>

5.效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结

两种方式非常相似,区别在于两点:
1、是否在地址后面拼接参数。
2、因为动态路由也是传递params的,所以在 this.$router.push() 方法中 path是不能和params一起使用的,否则params将无效。需要用name来指定页面,一定要记得,在index.js中设置好那么属性。

到此这篇关于this.$router.push携带参数跳转页面的文章就介绍到这了,更多相关this.$router.push跳转页面内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: this.$router.push携带参数跳转页面的实现代码

本文链接: https://www.lsjlt.com/news/202830.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作