queryParams 对象转URL参数
该方法,可以将一个对象形式参数转换成get
传参所需参数形式,如把{name: 'lisa', age: 20}
转换成?name=lisa&age=20
用途:可以用于uni.navigateTo
接口传参等场景,无需自己手动拼接URL参数
queryParams(data, isPrefix = true, arrayFormat = 'brackets')
data <Object>
对象值,如{name: 'lisa', age: 20}
isPrefix <Boolean>
是否在返回的字符串前加上"?",默认为true
arrayFormat <Boolean>
属性为数组的情况下的处理办法,默认为brackets
,见后面说明
uni.$m.queryParams({name: 'test', age: 18})
arrayFormat参数说明
如果您传入的data
对象内部某些属性值为数组的情况下,您可能需要留意这个参数的配置: 该参数可选值有4个:indices
,brackets
,repeat
,comma
,具体效果请见下方的演示说明
const data = {
name: 'test',
list: ['a', 'b', 'c', 'd']
}
// ?name=test&list[0]=a&list[1]=b&list[2]=c&list[3]=d
uni.$m.queryParams(data, true, 'indices')
// ?name=test&list[]=a&list[]=b&list[]=c&list[]=d
uni.$m.queryParams(data, true, 'brackets')
// ?name=test&list=a&list=b&list=c&list=d
uni.$m.queryParams(data, true, 'repeat')
// ?name=test&list=a,b,c,d
uni.$m.queryParams(data, true, 'comma')