-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathu-link.vue
88 lines (78 loc) · 2.11 KB
/
u-link.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<template>
<a class="u-link" v-on="listeners" :href="href" :disabled="disabled" @click="onClick">
<u-icon v-if="icon" :name="icon" />
<span v-if="$slots.default"> <slot></slot> </span>
</a>
</template>
<script>
export default {
name: 'u-link',
props: {
href: { type: String },
to: [String, Object],
replace: { type: Boolean, default: false },
append: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
icon: { type: String, default: '' }
},
computed: {
listeners() {
const listeners = Object.assign({}, this.$listeners)
delete listeners.click
return listeners
}
},
methods: {
onClick(e) {
if (this.disabled) return e.preventDefault()
this.$emit('click', e)
// 先执行事件,再执行to,最后判断href
this.navigate()
},
navigate() {
if (this.to === undefined) return
if (!this.$router) {
console.warn('Cannot find vue router.')
return
}
const $router = this.$router
const { location } = $router.resolve(this.to, this.$route, this.append)
this.replace ? $router.replace(location) : $router.push(location)
this.$emit('navigate', {
to: this.to,
exact: this.exact,
replace: this.replace
})
}
}
}
</script>
<style lang="scss" scoped>
.u-link {
display: inline-flex;
flex-direction: row;
align-items: center;
justify-content: center;
vertical-align: middle;
position: relative;
text-decoration: none;
cursor: pointer;
outline: none;
color: inherit;
@each $name, $color in $colors {
&[type='#{$name}'] {
color: $color;
}
}
&[disabled='disabled'] {
cursor: not-allowed;
color: $disabled-color;
}
& [class*='u-icon-'] + span {
margin-left: 5px;
}
.u-icon {
font-size: 14px;
}
}
</style>