Link.vue 700 字节
Newer Older
RuoYi's avatar
RuoYi committed
1
<template>
2
  <component :is="type" v-bind="linkProps(to)">
RuoYi's avatar
RuoYi committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    <slot />
  </component>
</template>

<script>
import { isExternal } from '@/utils/validate'

export default {
  props: {
    to: {
      type: String,
      required: true
    }
  },
17 18 19 20 21 22 23 24 25 26 27
  computed: {
    isExternal() {
      return isExternal(this.to)
    },
    type() {
      if (this.isExternal) {
        return 'a'
      }
      return 'router-link'
    }
  },
RuoYi's avatar
RuoYi committed
28
  methods: {
29 30
    linkProps(to) {
      if (this.isExternal) {
RuoYi's avatar
RuoYi committed
31
        return {
32
          href: to,
RuoYi's avatar
RuoYi committed
33 34 35 36 37
          target: '_blank',
          rel: 'noopener'
        }
      }
      return {
38
        to: to
RuoYi's avatar
RuoYi committed
39 40 41 42 43
      }
    }
  }
}
</script>