Skip to content

Commit 42d12b9

Browse files
committed
support named routes + overload router.go()
1 parent 33f2b3e commit 42d12b9

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const internalKeysRE = /^(component|subRoutes)$/
1+
const internalKeysRE = /^(component|subRoutes|name|fullPath)$/
22

33
/**
44
* Route Context Object

src/router/api.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,28 @@ export default function (Vue, Router) {
8080

8181
/**
8282
* Navigate to a given path.
83+
* The path can be an object describing a named path in
84+
* the format of { name: '...', params: {}, query: {}}
8385
* The path is assumed to be already decoded, and will
8486
* be resolved against root (if provided)
8587
*
86-
* @param {String} path
88+
* @param {String|Object} path
8789
* @param {Boolean} [replace]
8890
*/
8991

9092
Router.prototype.go = function (path, replace) {
93+
// handle named routes
94+
if (typeof path === 'object') {
95+
if (path.name) {
96+
var params = path.params || {}
97+
if (path.query) {
98+
params.queryParams = path.query
99+
}
100+
path = this._recognizer.generate(path.name, params)
101+
} else if (path.path) {
102+
path = path.path
103+
}
104+
}
91105
this.history.go(path + '', replace)
92106
}
93107

src/router/internal.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export default function (Vue, Router) {
2222
path: path,
2323
handler: handler
2424
})
25-
this._recognizer.add(segments)
25+
this._recognizer.add(segments, {
26+
as: handler.name
27+
})
28+
// add sub routes
2629
if (handler.subRoutes) {
2730
for (let subPath in handler.subRoutes) {
2831
// recursively walk all sub routes

0 commit comments

Comments
 (0)