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