Skip to content

Commit 681f649

Browse files
committed
add[components]: link
1 parent 92e9e29 commit 681f649

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

src/components/Link/index.vue

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<!-- eslint-disable vue/require-component-is -->
3+
<component v-bind="linkProps(to)">
4+
<slot></slot>
5+
</component>
6+
</template>
7+
8+
<script>
9+
import { isExternal } from '@/utils/validate.js'
10+
export default {
11+
name: 'Link',
12+
props: {
13+
to: {
14+
type: String,
15+
required: true
16+
}
17+
},
18+
methods: {
19+
linkProps (url) {
20+
if (isExternal(url)) {
21+
return {
22+
is: 'a',
23+
href: url,
24+
target: '_black',
25+
rel: 'noopener'
26+
}
27+
}
28+
return {
29+
is: 'router-link',
30+
to: url
31+
}
32+
}
33+
}
34+
}
35+
</script>
36+
37+
<style lang="scss" scoped>
38+
39+
</style>

src/lang/en.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
app: {
3-
welcome: 'Welcome to Your Vue.js App'
3+
welcome: 'Welcome to Your Vue.js App',
4+
vueOfficialWebsite: 'vue Official Website'
45
}
56
}

src/lang/zh.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
app: {
3-
welcome: '欢迎来到Vue世界'
3+
welcome: '欢迎来到Vue世界',
4+
vueOfficialWebsite: 'vue 官方网站'
45
}
56
}

src/utils/validate.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
* @param path
4+
* @returns {boolean}
5+
*/
6+
export function isExternal (path) {
7+
return /^(https?:|mailto:|tel:|ftp:)/.test(path)
8+
}

src/views/index.vue

+5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
<div>
33
<p>{{msg}}</p>
44
<p>{{$t('app.welcome')}}</p>
5+
<app-link to="https://vuejs.org/">{{$t('app.vueOfficialWebsite')}}</app-link>
56
</div>
67
</template>
78

89
<script>
10+
import AppLink from '@/components/Link/index'
911
export default {
1012
name: 'index',
1113
data () {
1214
return {
1315
msg: 'i am vue demo'
1416
}
17+
},
18+
components: {
19+
AppLink
1520
}
1621
}
1722
</script>

0 commit comments

Comments
 (0)