Vue路由params和query的区别

Vue路由params和query的区别:

项目基于iview-admin-master

params用法

• 只能使用命名的路由
• 地址栏不可见,请求类似 localhost:4396/user
• 在目标页面使用this.$route.params.id来获取
• 例如:

let route = {
  name: start-explore,
  params: {
    model_id: id,
    task_id: row.id,
  }
};
this.$router.push(route);

但是这种方式刷新后参数会丢失如果要保留的话,可在router.js中配置目标页面的path,在path后添加/:参数名/:···
例如:

{
  path: 'start-explore/:model_id/:task_id',
  name: 'start-explore',
  meta: {
    title: '开启配置',
    hideInMenu: true,
    icon: 'md-notifications'
  },
  component: () => import('@/view/model-explore/start-explore.vue')
},

Query用法

• 可使用命名或path的路由
• 地址栏可见,请求类似 localhost:4396/user?id=3&status=1
• 在目标页面使用this. r o u t e . q u e r y . i d 来 获 取 i d , 用 t h i s . route.query.id来获取id,用this. route.query.ididthis.route.query.status
• 例如:获取status

	   const route = {
		  name: 'show-task-base',
		  query: { id, status}
	   };
	   this.$router.push(route);

query传参方式刷新后数据不会丢失。

注意:

如果所传数据为对象(Object)需要转为字符串(JSON.stringify()),接收的时候再转为对象(JSON.parse()),不然会成[Object]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值