From 51b01483e43fd7ef8cff9cfa270b0c007b57e58c Mon Sep 17 00:00:00 2001 From: Konstantin Kitmanov Date: Sun, 20 Nov 2016 12:31:06 +0100 Subject: [PATCH] Add public Router#resolve method as per #831. Necessary docs, tests, typings. Fixes per code review. --- docs/en/api/router-instance.md | 4 ++++ src/components/link.js | 19 ++++--------------- src/index.js | 20 ++++++++++++++++++++ types/router.d.ts | 1 + 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/docs/en/api/router-instance.md b/docs/en/api/router-instance.md index 4bebc2a2f..8edc39593 100644 --- a/docs/en/api/router-instance.md +++ b/docs/en/api/router-instance.md @@ -39,3 +39,7 @@ - **router.getMatchedComponents()** Returns an Array of the components (definition/constructor, not instances) matched by the current route. This is mostly used during server-side rendering to perform data prefetching. + +- **router.resolve(location, current?, append?)** + + Reverse URL resolving. Given location in form same as used in ``, returns object with string property `href`. diff --git a/src/components/link.js b/src/components/link.js index ca4b1057a..54b85e545 100644 --- a/src/components/link.js +++ b/src/components/link.js @@ -1,8 +1,6 @@ /* @flow */ -import { cleanPath } from '../util/path' import { createRoute, isSameRoute, isIncludedRoute } from '../util/route' -import { normalizeLocation } from '../util/location' import { _Vue } from '../install' // work around weird flow bug @@ -27,14 +25,10 @@ export default { render (h: Function) { const router = this.$router const current = this.$route - const to = normalizeLocation(this.to, current, this.append) - const resolved = router.match(to, current) - const fullPath = resolved.redirectedFrom || resolved.fullPath - const base = router.history.base - const href = createHref(base, fullPath, router.mode) + const { normalizedTo, resolved, href } = router.resolve(this.to, current, this.append) const classes = {} const activeClass = this.activeClass || router.options.linkActiveClass || 'router-link-active' - const compareTarget = to.path ? createRoute(null, to) : resolved + const compareTarget = normalizedTo.path ? createRoute(null, normalizedTo) : resolved classes[activeClass] = this.exact ? isSameRoute(current, compareTarget) : isIncludedRoute(current, compareTarget) @@ -57,9 +51,9 @@ export default { e.preventDefault() if (this.replace) { - router.replace(to) + router.replace(normalizedTo) } else { - router.push(to) + router.push(normalizedTo) } } } @@ -106,8 +100,3 @@ function findAnchor (children) { } } } - -function createHref (base, fullPath, mode) { - var path = mode === 'hash' ? '/#' + fullPath : fullPath - return base ? cleanPath(base + path) : path -} diff --git a/src/index.js b/src/index.js index aba6ac0b6..50daf4471 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,8 @@ import { HTML5History, getLocation } from './history/html5' import { AbstractHistory } from './history/abstract' import { inBrowser, supportsHistory } from './util/dom' import { assert } from './util/warn' +import { cleanPath } from './util/path' +import { normalizeLocation } from './util/location' export default class VueRouter { static install: () => void; @@ -120,6 +122,24 @@ export default class VueRouter { }) })) } + + resolve (to: RawLocation, current?: Route, append?: boolean): {href: string} { + const normalizedTo = normalizeLocation(to, current || this.history.current, append) + const resolved = this.match(normalizedTo, current) + const fullPath = resolved.redirectedFrom || resolved.fullPath + const base = this.history.base + const href = createHref(base, fullPath, this.mode) + return { + normalizedTo, + resolved, + href + } + } +} + +function createHref (base: string, fullPath: string, mode) { + var path = mode === 'hash' ? '/#' + fullPath : fullPath + return base ? cleanPath(base + path) : path } VueRouter.install = install diff --git a/types/router.d.ts b/types/router.d.ts index c73b7ace8..03eae0853 100644 --- a/types/router.d.ts +++ b/types/router.d.ts @@ -28,6 +28,7 @@ declare class VueRouter { back (): void; forward (): void; getMatchedComponentes (): Component; + resolve (to: RawLocation, current?: Route, append?: boolean): {href: string}; static install: PluginFunction; }