Skip to content

Approach of conversion for array in query to avoid breaking change #817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 14, 2017
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/v2/guide/migration-vue-router.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ if (route.meta.requiresAuth) {
</div>
{% endraw %}

### [] Syntax for Array of Route Query <sup>removed</sup>

In URL, [] syntax for array of query is removed. For example, `/foo?users[]=Tom&users[]=Jerry` will be `/foo?users=Tom&users=Jerry` now.

Since vue-router will treat `?users=Tom` as `query.users == 'Tom'`, unless you are for sure that the array will have more than one element, you should check and convert the query to array if necessary.

One practical way is to add the following kind of watcher:

```javascript
'$route.query.users': {
handler(val) {
this.$route.query.users = Array.isArray(val) ? val : [val]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we talk about watchers? I think the relevant part is that the user should check if val is an array, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva This watcher can help to convert the query to an array no matter how many elements are provided in the url.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't talk about watchers, simply show the example like:

const users = Array.isArray(this.$route.query.users)
  ? users
  : [users]

Modifying the $route object is a bad idea because it's like a snapshot of the current state, it's supposed to be read only

},
immediate: true
}
```

## Route Matching

Route matching now uses [path-to-regexp](https://github.com/pillarjs/path-to-regexp) under the hood, making it much more flexible than previously.
Expand Down