【发布时间】:2021-10-04 21:58:48
【问题描述】:
我想通过以下方式从查询参数创建一个 url:
router.push(
{
pathname: '/cars',
query: colors + type + price,
},
undefined,
{
shallow: true,
},
);
const colors = ['red', 'blue', 'yellow']; //the arrays could contain many others values
const type = ['offroad', 'sport'];
const price = 'hight' //this is a string
我想实现,当我点击触发router.push的按钮时,下一个:
/cars?color=red,yellow,blue&type=offroad,sport&price=hight
【问题讨论】:
-
如果你有多个值的相同键,你将如何获得查询?
-
@Shyam,你是什么意思?
-
如果您的查询为 color=red&color=blue,您将如何解析它们?我建议您使用这种格式
/cars?colors=red,blue,green&type=sports -
@Shyam,我明白你的意思,但我需要针对上述情况的解决方案。你能帮忙吗?
-
查询参数必须是有效对象。在您的情况下,您有多个值的相同键,这将被覆盖。