🌐 AI搜索 & 代理 主页
Skip to content

Commit fad5a8b

Browse files
committed
fix(b-link): href handling with live router
1 parent 6f8aae0 commit fad5a8b

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/components/link/link.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export const BLink = /*#__PURE__*/ Vue.extend({
126126
computedHref() {
127127
// We don't pass `this` as the first arg as we need reactivity of the props
128128
const { to, href } = this
129-
return computeHref({ to, href })
129+
return computeHref({ to, href }, this.computedTag)
130130
},
131131
computedProps() {
132132
const { prefetch } = this

src/components/pagination-nav/pagination-nav.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export const BPaginationNav = /*#__PURE__*/ Vue.extend({
213213
try {
214214
// Convert the `to` to a HREF via a temporary `a` tag
215215
link = document.createElement('a')
216-
link.href = computeHref({ to }, '/', '/')
216+
link.href = computeHref({ to }, 'a', '/', '/')
217217
// We need to add the anchor to the document to make sure the
218218
// `pathname` is correctly detected in any browser (i.e. IE)
219219
document.body.appendChild(link)

src/utils/router.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { isArray, isNull, isPlainObject, isString, isUndefined } from './inspect
44
import { keys } from './object'
55
import { toString } from './string'
66

7+
const ANCHOR_TAG = 'a'
8+
79
// Method to replace reserved chars
810
const encodeReserveReplacer = c => '%' + c.charCodeAt(0).toString(16)
911

@@ -88,7 +90,7 @@ export const isRouterLink = tag => !!(tag && !isTag(tag, 'a'))
8890
export const computeTag = ({ to, disabled, routerComponentName } = {}, thisOrParent) => {
8991
const hasRouter = !!thisOrParent.$router
9092
if (!hasRouter || (hasRouter && (disabled || !to))) {
91-
return 'a'
93+
return ANCHOR_TAG
9294
}
9395

9496
// TODO:
@@ -105,12 +107,24 @@ export const computeTag = ({ to, disabled, routerComponentName } = {}, thisOrPar
105107
export const computeRel = ({ target, rel } = {}) =>
106108
target === '_blank' && isNull(rel) ? 'noopener' : rel || null
107109

108-
export const computeHref = ({ href, to } = {}, fallback = '#', toFallback = '/') => {
110+
export const computeHref = (
111+
{ href, to } = {},
112+
tag = ANCHOR_TAG,
113+
fallback = '#',
114+
toFallback = '/'
115+
) => {
109116
// Return `href` when explicitly provided
110117
if (href) {
111118
return href
112119
}
113120

121+
// We've checked for `$router` in `computeTag()`, so `isRouterLink()` indicates a live router
122+
// When deferring to Vue Router's `<router-link>`, don't use the `href` attribute at all
123+
// We return `null`, and then remove `href` from the attributes passed to `<router-link>`
124+
if (isRouterLink(tag)) {
125+
return null
126+
}
127+
114128
// Fallback to `to` prop (if `to` is a string)
115129
if (isString(to)) {
116130
return to || toFallback

src/utils/router.spec.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ describe('utils/router', () => {
110110

111111
it('parses nothing to default', async () => {
112112
expect(computeHref()).toEqual('#')
113-
expect(computeHref(undefined, '/', '')).toEqual('/')
114-
expect(computeHref(undefined, '', '')).toEqual('')
113+
expect(computeHref(undefined, undefined, '/', '')).toEqual('/')
114+
expect(computeHref(undefined, undefined, '', '')).toEqual('')
115+
})
116+
117+
it('returns null when tag is not `a`', async () => {
118+
expect(computeHref({}, 'div')).toEqual(null)
119+
expect(computeHref(undefined, 'div', '/', '')).toEqual(null)
120+
expect(computeHref(undefined, 'span', '', '/')).toEqual(null)
115121
})
116122

117123
it('returns href when both href and to provided', async () => {
@@ -124,8 +130,8 @@ describe('utils/router', () => {
124130

125131
it('parses empty `href` to default', async () => {
126132
expect(computeHref({ href: '' })).toEqual('#')
127-
expect(computeHref({ href: '' }, '/', '')).toEqual('/')
128-
expect(computeHref({ href: '' }, '', '')).toEqual('')
133+
expect(computeHref({ href: '' }, 'a', '/', '')).toEqual('/')
134+
expect(computeHref({ href: '' }, 'a', '', '')).toEqual('')
129135
})
130136

131137
it('parses `to` when string', async () => {
@@ -173,8 +179,8 @@ describe('utils/router', () => {
173179

174180
it('parses empty `to` to fallback default', async () => {
175181
expect(computeHref({ to: {} })).toEqual('#')
176-
expect(computeHref({ to: {} }, '#', '')).toEqual('#')
177-
expect(computeHref({ to: {} }, '/', '#')).toEqual('/')
182+
expect(computeHref({ to: {} }, 'a', '#', '')).toEqual('#')
183+
expect(computeHref({ to: {} }, 'a', '/', '#')).toEqual('/')
178184
})
179185

180186
it('parses complete `to`', async () => {
@@ -197,7 +203,6 @@ describe('utils/router', () => {
197203
// isRouterLink() utility method
198204
describe('isRouterLink()', () => {
199205
it('works', async () => {
200-
expect(isRouterLink('a')).toBe(false)
201206
expect(isRouterLink('router-link')).toBe(true)
202207
expect(isRouterLink('nuxt-link')).toBe(true)
203208
expect(isRouterLink()).toBe(false)

0 commit comments

Comments
 (0)