【转】vue中get请求如何传递数组参数

前言

vue 中在与后端进行数据交互时,使用 axios 发送请求,不做配置直接使用get请求传递数组类型参数的时候,后端是无法接收数据的,需要对 axios 一些简单的配置才能让后端完美的接收数组

1、问题

示例代码

let params = {
                statusList: ['OVERDUE', 'DELAY']
             }
             
this.$http.get('/list', params)
            .then(res => {})
            .catch(e => {})

上述代码在不做配置的时候请求信息为: /list?statusList[]=OVERDUE&statusList[]=DELAY 对于后端来说, statusList[] 形式的提交是无效的,实际需要的是 /list?statusList=OVERDUE&statusList=DELAY 这种方式的提交。那么我们应该如何来解决这种问题呢?

2、解决方案

2.1 qs 插件

qs 主要是增加一些安全性的查询字符串解析和序列化字符串的库,qs 的更多使用方式可以参考总结中提供的地址学习

1、qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
// 输出结果:'a[0]=b&a[1]=c'
2、qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
// 输出结果:'a[]=b&a[]=c'
3、qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
// 输出结果:'a=b&a=c'
4、qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 输出结果:'a=b,c'

上述我们列举了qs中的序列化几种配置,其中 { arrayFormat: 'repeat' } 的序列化结果满足我们的条件

2.2 axios 配置

axios 中有一个专门对数据进行序列化的配置属性 paramsSerializer

 paramsSerializer: function(params) {
   return Qs.stringify(params, {arrayFormat: 'repeat'})
 },

2.3 具体配置

我们可以在 axios 请求拦截器中对参数进行序列化配置

axios.interceptors.request.use(async (config) => {
//只针对get方式进行序列化
 if (config.method === 'get') {
   config.paramsSerializer = function(params) {
     return qs.stringify(params, { arrayFormat: 'repeat' })
   }
 }
}

3、总结

axios中文文档:https://www.kancloud.cn/yunye/axios/234845
qs插件:https://www.npmjs.com/package/qs


转自:[https://www.jianshu.com/p/a3179dbf5b43](https://www.jianshu.com/p/a3179dbf5b43)
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Vue,父组件向子组件传递数组可以使用props属性。以下是实现方法: 1. 在父组件定义数组并向子组件传递: ```html <template> <div> <child-component :list="myList"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { myList: ['item1', 'item2', 'item3'] } } } </script> ``` 2. 在子组件接收数组: ```html <template> <div> <ul> <li v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> </div> </template> <script> export default { props: { list: { type: Array, required: true } } } </script> ``` 在子组件,我们使用了v-for指令遍历父组件传递数组,并将每个元素显示在页面上。注意,我们在props定义了list属性的类型为Array,并设置了required属性为true,表示这是必须的属性,父组件必须传递这个数组给子组件。 ### 回答2: 在Vue,父组件传递数组给子组件的方法有多种。下面会介绍两种典型的方式。 第一种方式是通过props将数组传递给子组件。在父组件,可以使用v-bind指令将数组绑定到子组件定义的props属性上。例如,在父组件template,可以使用如下代码将数组传递给子组件: ```html <template> <div> <child-component :arrayProp="myArray"></child-component> </div> </template> ``` 然后,在子组件,通过props声明接收该数组数据。例如,在子组件的props添加如下代码: ```javascript props: { arrayProp: { type: Array, required: true } } ``` 这样子组件就能够访问并使用父组件传递过来的数组。 第二种方式是通过Vuex来进行状态管理。在父组件,将数组存储在Vuex的store,然后在子组件通过computed属性来获取该数组。首先,需要安装Vuex并在项目创建一个store。然后,在store定义一个数组状态: ```javascript // store.js import Vuex from 'vuex' import Vue from 'vue' Vue.use(Vuex) const store = new Vuex.Store({ state: { myArray: [1, 2, 3, 4] } }) export default store ``` 在父组件,通过使用mapState辅助函数来获取store数组数据: ```javascript import { mapState } from 'vuex' export default { computed: { ...mapState(['myArray']) } } ``` 然后在子组件,可以直接访问父组件传递数组数据: ```javascript <template> <div>{{ myArray }}</div> </template> ``` 通过以上两种方式,父组件就可以将数组数据成功传递给子组件,并在子组件使用该数组数据。 ### 回答3: 在Vue,父组件向子组件传递数组的方式有很多种方法。我将介绍其两种常见的方法。 第一种方法是通过props属性传递数组。在父组件,可以在子组件的标签通过v-bind指令将数组以props的形式传递给子组件。具体的做法是,在子组件标签添加一个名为data的props,并将父组件数组赋值给这个props。在子组件,可以使用this.data来访问这个数组。 例如,父组件的代码如下: ``` <template> <div> <child-component :data="array"></child-component> </div> </template> <script> export default { data() { return { array: [1, 2, 3, 4, 5] } } } </script> ``` 在子组件的代码如下: ``` <template> <div> <ul> <li v-for="item in data" :key="item">{{ item }}</li> </ul> </div> </template> <script> export default { props: { data: { type: Array, required: true } } } </script> ``` 第二种方法是使用事件传递数组。在父组件,可以在子组件标签使用v-on指令监听一个事件,并在事件处理函数将父组件数组作为参数传递给子组件。在子组件,可以在父组件传递的事件接收到这个数组。 例如,父组件的代码如下: ``` <template> <div> <child-component @receive-array="receiveArray"></child-component> </div> </template> <script> export default { data() { return { array: [1, 2, 3, 4, 5] } }, methods: { receiveArray(arr) { // 在这里可以对传递过来的数组进行处理 console.log(arr); } } } </script> ``` 在子组件的代码如下: ``` <template> <div> <button @click="sendArray">发送数组</button> </div> </template> <script> export default { methods: { sendArray() { this.$emit('receive-array', [1, 2, 3, 4, 5]); // 在这里可以将数组发送给父组件 } } } </script> ``` 以上就是父组件传递数组给子组件的两种常见方法。你可以根据实际需求选择适合的方法来传递数组

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值