Link.vue 657 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <template>
  2. <component :is="type" v-bind="linkProps(to)">
  3. <slot />
  4. </component>
  5. </template>
  6. <script>
  7. import { isExternal } from '@/utils/validate'
  8. export default {
  9. props: {
  10. to: {
  11. type: String,
  12. required: true
  13. }
  14. },
  15. computed: {
  16. isExternal() {
  17. return isExternal(this.to)
  18. },
  19. type() {
  20. if (this.isExternal) {
  21. return 'a'
  22. }
  23. return 'router-link'
  24. }
  25. },
  26. methods: {
  27. linkProps(to) {
  28. if (this.isExternal) {
  29. return {
  30. href: to,
  31. target: '_blank',
  32. rel: 'noopener'
  33. }
  34. }
  35. return {
  36. to: to
  37. }
  38. }
  39. }
  40. }
  41. </script>