index.vue 684 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <template>
  2. <component :is="linkType" v-bind="linkProps(to)">
  3. <slot />
  4. </component>
  5. </template>
  6. <script setup lang="ts">
  7. defineOptions({
  8. name: "AppLink",
  9. inheritAttrs: false,
  10. });
  11. import { isExternal } from "@/utils/index";
  12. const props = defineProps({
  13. to: {
  14. type: Object,
  15. required: true,
  16. },
  17. });
  18. const isExternalLink = computed(() => {
  19. return isExternal(props.to.path || "");
  20. });
  21. const linkType = computed(() => (isExternalLink.value ? "a" : "router-link"));
  22. const linkProps = (to: any) => {
  23. if (isExternalLink.value) {
  24. return {
  25. href: to.path,
  26. target: "_blank",
  27. rel: "noopener noreferrer",
  28. };
  29. }
  30. return { to };
  31. };
  32. </script>