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="/"> <MyNuxtLink to="/">
Index page with a custom link component with a custom active class Index page with a custom link component with a custom active class
</MyNuxtLink> </MyNuxtLink>
<NuxtLink>Link without href and to</NuxtLink>
</div> </div>
</template> </template>

View File

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

View File

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