Skip to content

Commit c57dc8f

Browse files
fix: add fixes from PR #940
1 parent 80e84ba commit c57dc8f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/components/link.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,29 @@ export const computed = {
8585
}
8686
};
8787

88-
function computeTag(props) {
89-
return props.to && !props.disabled ? "router-link" : "a";
88+
function computeTag(props, parent) {
89+
return Boolean(parent.$router) && props.to && !props.disabled ? "router-link" : "a";
9090
}
9191

9292
function computeHref({ disabled, href, to }, tag) {
93+
// We've already checked the parent.$router in computeTag,
94+
// so router-link means live router.
9395
// When deferring to Vue Router's router-link,
9496
// don't use the href attr at all.
95-
if (tag === "router-link") return null;
97+
// Must return undefined for router-link to populate href.
98+
if (tag === "router-link") return void 0;
9699
if (disabled) return "#";
97100
// If href explicitly provided
98101
if (href) return href;
102+
// Reconstruct href when `to` used, but no router
103+
if (to) {
104+
// Fallback to `to` prop (if `to` is a string)
105+
if (typeof to === "string") return to;
106+
// Fallback to `to.path` prop (if `to` is an object)
107+
if (typeof to === "object" && typeof to.path === "string") return to.path;
108+
}
99109
// If nothing is provided use '#'
100-
return '#'
110+
return "#";
101111
}
102112

103113
function computeRel({ target, rel }) {
@@ -138,7 +148,7 @@ export default {
138148
functional: true,
139149
props: propsFactory(),
140150
render(h, { props, data, parent, children }) {
141-
const tag = computeTag(props),
151+
const tag = computeTag(props, parent),
142152
rel = computeRel(props),
143153
href = computeHref(props, tag),
144154
eventType = tag === "router-link" ? "nativeOn" : "on",

0 commit comments

Comments
 (0)