Created
July 30, 2021 10:16
-
-
Save AnoRebel/84b0b241255fe49c5611204887c30746 to your computer and use it in GitHub Desktop.
Vue 3 dynamic link for both external and internal links(with vue-router)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<a v-if="isExternal" :href="to" target="_blank" rel="noopener"><slot /></a> | |
<router-link v-else v-bind="$props"><slot /></router-link> | |
</template> | |
<script> | |
import { computed } from "vue"; | |
import { RouterLink } from "vue-router"; | |
export default { | |
props: { | |
...RouterLink.props, | |
}, | |
setup(props) { | |
const isExternal = computed(() => typeof props.to === "string" && props.to.startsWith("http")); | |
return { isExternal }; | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment