fix(nuxt-link): handle state without href and to

This commit is contained in:
Pooya Parsa 2022-03-16 11:10:32 +01:00
parent 04026a3861
commit 970a4d36d5
3 changed files with 8 additions and 6 deletions

View File

@ -21,5 +21,6 @@
<MyNuxtLink to="/">
Index page with a custom link component with a custom active class
</MyNuxtLink>
<NuxtLink>Link without href and to</NuxtLink>
</div>
</template>

View File

@ -141,8 +141,7 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
return false
}
// Directly check if `to` is an external URL by checking protocol
return hasProtocol(to.value, true)
return to.value === '' || hasProtocol(to.value, true)
})
return () => {
@ -171,10 +170,10 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
// Resolves `rel`
checkPropConflicts(props, 'noRel', 'rel')
const rel = props.noRel
const rel = (props.noRel)
? null
// converts `""` to `null` to prevent the attribute from being added as empty (`rel=""`)
: firstNonUndefined<string | null>(props.rel, options.externalRelAttribute, DEFAULT_EXTERNAL_REL_ATTRIBUTE) || null
: firstNonUndefined<string | null>(props.rel, options.externalRelAttribute, href ? DEFAULT_EXTERNAL_REL_ATTRIBUTE : '') || null
return h('a', { href, rel, target }, slots.default())
}

View File

@ -53,8 +53,10 @@ describe('nuxt-link:to', () => {
consoleWarnSpy.mockRestore()
})
it('defaults to `null`', () => {
expect(nuxtLink().props.href).toBe(null)
it('without to and href', () => {
const link = nuxtLink()
expect(link.props.href).toBe(null)
expect(link.props.rel).toBe(null)
})
})