简体   繁体   English

在 VUE.JS 中传递多个参数问题

[英]Passing multiple params issue in VUE.JS

I am trying to get data by passing multiple ids as param.But the params i am passing is going as array which i don't want.我试图通过将多个 id 作为参数传递来获取数据。但是我传递的参数是我不想要的数组。

Example:例子:

Current data: The api is returning param as details?ids[]=123&ids[]=245当前数据:api 返回参数作为 details?ids[]=123&ids[]=245

Expected data: I would like to pass the param as details?ids=123&ids=245预期数据:我想将参数作为详细信息传递?ids=123&ids=245

  const postRes = await axios.post('employee/departments', this.details);
  const getRes = await axios.get('employee/departments',
     { params: { ids: postRes.map(i=>i.id)},
  });
  this.$emit('fileDetails', getRes.data);

You could serialize the parameters yourself:您可以自己序列化参数:

  1. Map each object into a string, containing 'ids=' prefixed to the object's id . Map 将每个 object 转换为一个字符串,其中包含以对象id为前缀的'ids='
  2. Join the resulting array with a & delimiter.使用&分隔符连接生成的数组。
const params = postRes.map(i => 'ids=' + i.id) 1️⃣
                      .join('&') 2️⃣
const getRes = await axios.get('employee/departments?' + params)

Run this snippet for example output:例如,运行此代码段 output:

 const postRes = [{ id: 11 }, { id: 22 }, { id: 33 }] const url = 'employee/departments?' + postRes.map(i => 'ids=' + i.id).join('&') console.log(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM