vue router传递参数 params和query

[size=medium][color=blue][b]需求:
list.vue页面
是这样子的,我想把itemId传给hotel.vue页面。
点击url会跳转到http://localhost:8080/#/hotel/1001
分为参数显示在url和参数隐藏


1、参数显示在url中
--------------------------------------------------------------------
list.vue 中
<template>
<div>
<h3>首页</h3>
<router-link :to="{path:'/hotel', query: {itemId:1001}}">
<button>显示</button>
</router-link>
<router-view></router-view>
</div>
</template>
url:http://localhost:8080/#/hotel?itemId=1001

hotel.vue 中
this.$route.query.itemId
F5强制刷新下hotel.vue页面,itemId参数依然存在


2、参数不显示在url中,如果在PC端将传递的值显示在url中,这样无形中就存在安全隐患,如果客户不小心修改了url那样就会出错,移动端就无所谓了,如何才能不显示在url中,同样很简单,但是需要给映射的路径起一个别名,通过name来取别名
--------------------------------------------------------------------
同样只需将上面的main.js中的定义路由改为如下样子,在子路由中通过name来给路径其一个game1的别名。
//定义路由
const routes = [
{ path: "/", redirect: "/home" },//重定向
{
path: "/home", component: home,
children: [
{ name: "game1", path: "/home/game/", component: game }
]
}
]

list.vue 中
router-link修改为:to="{ name:'game1', params: {itemId: 1001} }"
params中是要传递的参数,这样传递的参数就不会显示在url中。

<template>
<div>
<h3>首页</h3>
<router-link :to="{ name:'game1', params: {itemId: 1001} }">
<button>显示</button>
</router-link>
<router-view></router-view>
</div>
</template>
url:http://localhost:8080/#/hotel

hotel.vue 中
this.$route.params.itemId
可是F5强制刷新下hotel.vue页面,itemId参数就丢失了

[/b][/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值