-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Remove extras from old params in named routes #910
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,10 @@ const regexpCache: { | |
} | ||
} = Object.create(null) | ||
|
||
const regexpParamsCache: { | ||
[key: string]: Array<string> | ||
} = Object.create(null) | ||
|
||
const regexpCompileCache: { | ||
[key: string]: Function | ||
} = Object.create(null) | ||
|
@@ -31,14 +35,15 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher { | |
|
||
if (name) { | ||
const record = nameMap[name] | ||
const paramNames = getParams(record.path) | ||
|
||
if (typeof location.params !== 'object') { | ||
location.params = {} | ||
} | ||
|
||
if (currentRoute && typeof currentRoute.params === 'object') { | ||
for (const key in currentRoute.params) { | ||
if (!(key in location.params)) { | ||
if (!(key in location.params) && paramNames.indexOf(key) > -1) { | ||
location.params[key] = currentRoute.params[key] | ||
} | ||
} | ||
|
@@ -198,6 +203,26 @@ function fillParams ( | |
} | ||
} | ||
|
||
function getParams (path: string): Array<string> { | ||
const hit = regexpParamsCache[path] | ||
|
||
if (hit) { | ||
return hit | ||
} | ||
|
||
const keys = [] | ||
const paramNames: Array<string> = [] | ||
|
||
Regexp(path, keys) | ||
|
||
for (let i = 0, len = keys.length; i < len; ++i) { | ||
const key = keys[i] | ||
if (key && 'name' in key) paramNames.push(key.name) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole for-loop can be simplified to |
||
|
||
return (regexpParamsCache[path] = paramNames) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can then be simplified to return regexpParamsCache[path] ||
(regexpParamsCache[path] = keys.map(k => k.name)) |
||
} | ||
|
||
function resolveRecordPath (path: string, record: RouteRecord): string { | ||
return resolvePath(path, record.parent ? record.parent.path : '/', true) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have to call
Regexp(path)
here again (without cache, actually), there's already code for getting cached keys and regexp here (https://github.com/znck/vue-router/blob/ab0a779ba6f09f5b28138c9445ce84d7263daef6/src/create-matcher.js#L163-L172).We should probably create a separate function that uses the code above, take a
path
as only parameter, returns a{keys, regexp}
object, maybe name itgetKeysAndParams
.Then we can get keys with ES6 destructuring