首页 > 解决方案 > 路由参数中的数组导致“预期不会重复”警告

问题描述

当我尝试router-link使用数组作为参数创建一个时,链接有效,但我收到以下警告:

命名路由“start-run”的缺少参数:预期的“文件”不会重复,但已收到["aaa"]

路由器.js

...
{
  path: '/start-run/:config?/:files?',
  name: 'start-run',
  component: StartRun,
  props: true
},
...

文件.vue

...
<router-link :to="{name: 'start-run', params: { config: 'test', files: ['aaa'] }}">...</router-link>
...

我没有找到解决此警告的文档。

标签: javascriptvue.jsvue-router

解决方案


我相信错误消息来自path-to-regexpVue Router 使用的 1.7.0 版本:

https://github.com/pillarjs/path-to-regexp/blob/v1.7.0/index.js#L185

问题是数组被视为多个值,但路径中的参数不支持多个值。

目前尚不清楚通过将数组传递给files. 您定义的路由路径使用?后缀 for :files?,这使其成为可选但不允许使用数组。也许你的意思是:files*或者:files+相反?或者,也许您只想传递数组中的第一项?


推荐阅读