vp-link.vue 718 B

123456789101112131415161718192021222324252627282930
  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 :is="href ? 'a' : 'span'" class="link-item" :class="{ link: href }" :href="href" :target="isExternal ? '_blank' : undefined" :rel="isExternal ? 'noopener noreferrer' : undefined">
  11. <slot></slot>
  12. <ElIcon v-if="isExternal && !noIcon">
  13. <i-ri-external-link-line class="link-icon" />
  14. </ElIcon>
  15. </component>
  16. </template>
  17. <style scoped>
  18. .link-item {
  19. display: flex;
  20. align-items: center;
  21. }
  22. .el-icon {
  23. margin-left: 4px;
  24. }
  25. </style>