vp-link.vue 708 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <script setup lang="ts">
  2. import { computed } from 'vue'
  3. const props = defineProps<{
  4. href?: string
  5. noIcon?: boolean
  6. }>()
  7. const isExternal = computed(() => props.href && /^[a-z]+:/i.test(props.href))
  8. </script>
  9. <template>
  10. <component
  11. :is="href ? 'a' : 'span'"
  12. class="link-item"
  13. :class="{ link: href }"
  14. :href="href"
  15. :target="isExternal ? '_blank' : undefined"
  16. :rel="isExternal ? 'noopener noreferrer' : undefined"
  17. >
  18. <slot />
  19. <ElIcon v-if="isExternal && !noIcon">
  20. <i-ri-external-link-line class="link-icon" />
  21. </ElIcon>
  22. </component>
  23. </template>
  24. <style scoped>
  25. .link-item {
  26. display: flex;
  27. align-items: center;
  28. }
  29. .el-icon {
  30. margin-left: 4px;
  31. }
  32. </style>