From effb1143db10376c4ed553aec9a909dd1430ef66 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 11 Oct 2017 17:43:46 -0400 Subject: [PATCH] fix: deep clone query when creating routes fix #1690 --- src/util/query.js | 3 +-- src/util/route.js | 22 +++++++++++++++++++++- test/unit/specs/query.spec.js | 9 --------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/util/query.js b/src/util/query.js index c2e203d47..b9abc211f 100644 --- a/src/util/query.js +++ b/src/util/query.js @@ -29,8 +29,7 @@ export function resolveQuery ( parsedQuery = {} } for (const key in extraQuery) { - const val = extraQuery[key] - parsedQuery[key] = Array.isArray(val) ? val.slice() : val + parsedQuery[key] = extraQuery[key] } return parsedQuery } diff --git a/src/util/route.js b/src/util/route.js index c87dfe8a8..54a91a738 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -12,12 +12,18 @@ export function createRoute ( router?: VueRouter ): Route { const stringifyQuery = router && router.options.stringifyQuery + + let query: any = location.query || {} + try { + query = clone(query) + } catch (e) {} + const route: Route = { name: location.name || (record && record.name), meta: (record && record.meta) || {}, path: location.path || '/', hash: location.hash || '', - query: location.query || {}, + query, params: location.params || {}, fullPath: getFullPath(location, stringifyQuery), matched: record ? formatMatch(record) : [] @@ -28,6 +34,20 @@ export function createRoute ( return Object.freeze(route) } +function clone (value) { + if (Array.isArray(value)) { + return value.map(clone) + } else if (value && typeof value === 'object') { + const res = {} + for (const key in value) { + res[key] = clone(value[key]) + } + return res + } else { + return value + } +} + // the starting route that represents the initial state export const START = createRoute(null, { path: '/' diff --git a/test/unit/specs/query.spec.js b/test/unit/specs/query.spec.js index 040e45541..f5760452a 100644 --- a/test/unit/specs/query.spec.js +++ b/test/unit/specs/query.spec.js @@ -9,15 +9,6 @@ describe('Query utils', () => { baz: 'qux' })) }) - - it('should make a copy when param value is an array', () => { - const arr = ['bar'] - const query = resolveQuery('', { foo: arr }) - arr.push('baz') - expect(JSON.stringify(query)).toBe(JSON.stringify({ - foo: ['bar'] - })) - }) }) describe('stringifyQuery', () => {