Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/en/api/router-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<router-link/>`, returns object with string property `href`.
19 changes: 4 additions & 15 deletions src/components/link.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -57,9 +51,9 @@ export default {

e.preventDefault()
if (this.replace) {
router.replace(to)
router.replace(normalizedTo)
} else {
router.push(to)
router.push(normalizedTo)
}
}
}
Expand Down Expand Up @@ -106,8 +100,3 @@ function findAnchor (children) {
}
}
}

function createHref (base, fullPath, mode) {
var path = mode === 'hash' ? '/#' + fullPath : fullPath
return base ? cleanPath(base + path) : path
}
20 changes: 20 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions types/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<never>;
}
Expand Down