Skip to content

Commit effb114

Browse files
committed
fix: deep clone query when creating routes
fix #1690
1 parent 3cc08fb commit effb114

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/util/query.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ export function resolveQuery (
2929
parsedQuery = {}
3030
}
3131
for (const key in extraQuery) {
32-
const val = extraQuery[key]
33-
parsedQuery[key] = Array.isArray(val) ? val.slice() : val
32+
parsedQuery[key] = extraQuery[key]
3433
}
3534
return parsedQuery
3635
}

src/util/route.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ export function createRoute (
1212
router?: VueRouter
1313
): Route {
1414
const stringifyQuery = router && router.options.stringifyQuery
15+
16+
let query: any = location.query || {}
17+
try {
18+
query = clone(query)
19+
} catch (e) {}
20+
1521
const route: Route = {
1622
name: location.name || (record && record.name),
1723
meta: (record && record.meta) || {},
1824
path: location.path || '/',
1925
hash: location.hash || '',
20-
query: location.query || {},
26+
query,
2127
params: location.params || {},
2228
fullPath: getFullPath(location, stringifyQuery),
2329
matched: record ? formatMatch(record) : []
@@ -28,6 +34,20 @@ export function createRoute (
2834
return Object.freeze(route)
2935
}
3036

37+
function clone (value) {
38+
if (Array.isArray(value)) {
39+
return value.map(clone)
40+
} else if (value && typeof value === 'object') {
41+
const res = {}
42+
for (const key in value) {
43+
res[key] = clone(value[key])
44+
}
45+
return res
46+
} else {
47+
return value
48+
}
49+
}
50+
3151
// the starting route that represents the initial state
3252
export const START = createRoute(null, {
3353
path: '/'

test/unit/specs/query.spec.js

-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ describe('Query utils', () => {
99
baz: 'qux'
1010
}))
1111
})
12-
13-
it('should make a copy when param value is an array', () => {
14-
const arr = ['bar']
15-
const query = resolveQuery('', { foo: arr })
16-
arr.push('baz')
17-
expect(JSON.stringify(query)).toBe(JSON.stringify({
18-
foo: ['bar']
19-
}))
20-
})
2112
})
2213

2314
describe('stringifyQuery', () => {

0 commit comments

Comments
 (0)