Skip to content

Commit 36f2067

Browse files
committed
add support for query options when using path syntax in v-link and router.go (close #205)
1 parent 339b7d3 commit 36f2067

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

lib/route-recognizer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,13 @@ RouteRecognizer.prototype = {
415415
if (output.charAt(0) !== '/') { output = '/' + output; }
416416

417417
if (params && params.queryParams) {
418-
output += this.generateQueryString(params.queryParams, route.handlers);
418+
output += this.generateQueryString(params.queryParams);
419419
}
420420

421421
return output;
422422
},
423423

424-
generateQueryString: function(params, handlers) {
424+
generateQueryString: function(params) {
425425
var pairs = [];
426426
var keys = [];
427427
for(var key in params) {

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,22 @@ class Router {
530530
_stringifyPath (path) {
531531
if (path && typeof path === 'object') {
532532
if (path.name) {
533-
var params = path.params || {}
533+
const params = path.params || {}
534534
if (path.query) {
535535
params.queryParams = path.query
536536
}
537537
return this._recognizer.generate(path.name, params)
538538
} else if (path.path) {
539-
return path.path
539+
let fullPath = path.path
540+
if (path.query) {
541+
const query = this._recognizer.generateQueryString(path.query)
542+
if (fullPath.indexOf('?') > -1) {
543+
fullPath += '&' + query.slice(1)
544+
} else {
545+
fullPath += query
546+
}
547+
}
548+
return fullPath
540549
} else {
541550
return ''
542551
}

test/unit/specs/core.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,18 @@ describe('Core', function () {
244244
component: {
245245
template:
246246
'<div>' +
247-
'<a id="link-a" v-link="{ path: \'b\' }">Link A</a>' +
247+
'<a id="link-a" v-link="{ path: \'b\', query: { id: 123 }}">Link A</a>' +
248248
'</div>'
249249
}
250250
},
251251
'/b': {
252252
component: {
253253
data: function () {
254-
return { a: 'a' }
254+
return { a: 'a?a=1' }
255255
},
256256
template:
257257
'<div>' +
258-
'<a id="link-b" v-link="{ path: \'/\' + a }">Link B</a>' +
258+
'<a id="link-b" v-link="{ path: \'/\' + a, query: { b: 2 }}">Link B</a>' +
259259
'<a id="link-c" v-link="{ path: c }"></c>' +
260260
'</div>'
261261
}
@@ -283,12 +283,12 @@ describe('Core', function () {
283283
nextTick(function () {
284284
expect(el.textContent).toBe('Link A')
285285
var link = el.querySelector('#link-a')
286-
expect(link.getAttribute('href')).toBe('b')
286+
expect(link.getAttribute('href')).toBe('b?id=123')
287287
click(link)
288288
nextTick(function () {
289289
expect(el.textContent).toBe('Link B')
290290
var link = el.querySelector('#link-b')
291-
expect(link.getAttribute('href')).toBe('/a')
291+
expect(link.getAttribute('href')).toBe('/a?a=1&b=2')
292292
// falsy expressions should not set href
293293
expect(el.querySelector('#link-c').hasAttribute('href')).toBe(false)
294294
click(link)

0 commit comments

Comments
 (0)