Vue.js之this.$route.params和this.$route.query的区别(附代码)

本文详细介绍了Vue.js中路由参数的两种传递方式:query和params。通过示例展示了如何在router/index.js配置路由,如何在组件中传递和获取参数,并分析了它们在URL中表现形式的区别。query参数会显示在URL中,而params参数则不会。

this.$route.query的使用

1、在router/index.js文件进行编辑

{
 path:'/index',
 component: index,
 //添加路由
 children:[
   {
    path:':shopid',
    component:detail
   }
 ]     

},

2、传参数

this.$router.push({
        path: '/index/detail', 
        query:{shopid: item.id}
        });

3、获取参数

this.$route.query.shopid

4、url的表现形式(url中带有参数)

http://localhost:8080/#/index/detail?shopid=1

this.$route.params的使用

1、router/index.js

{
     path:'/index',
     component: index,
     //添加路由
     children:[
       {
        path:"/detail",
        name:'detail',
        component:detail
       }
     ]     

    },

2、传参数( params相对应的是namequery相对应的是path

this.$router.push({
        name: 'detail', 
        params:{shopid: item.id}
        });

3、获取参数

this.$route.params.shopid

4、url的表现形式(url中没带参数)

http://localhost:8080/#/index

还有另外一种可以看到参数的方式

1.传参数

{
        path: '/Page/:shopid/:abc',
        name: 'Page',
        component: Page
    }

2、获取参数

this.$route.params.shopid
this.$route.params.abc

3、url的表现形式(url中带参数)

http://localhost:8080/#/index/123/456
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值