创建项目

npx @vue/cli create xy-ui
https://ref.leonus.cn/docs/vue2.html?spm=wolai.workspace.0.0.388d41dbM4GcDg

componet下创建 Button/button.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
<template>
<button class="xy-button">
<slot/>
</button>
</template>

<script>
export default {
// eslint-disable-next-line vue/multi-word-component-names
name: "XyButton",
props: {},
components: {},
data() {
return {

}
},
computed: {},
methods: {},
};
</script>

<style lang="scss" scoped>

</style>

注册组件

1
2
3
4
5
6
7
8
9
10
11
12
13
import Vue from 'vue'
import App from './App.vue'

import XyButton from '@/components/Button/button.vue'
Vue.config.productionTip = false

// 第二步:注册组件,设置(组件名,组件)
Vue.component(XyButton.name, XyButton)

new Vue({
render: h => h(App),
}).$mount('#app')

使用组件

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
<template>
<div id="app">
<XyButton>登录</XyButton>
<XyButton>确认</XyButton>
<XyButton>取消</XyButton>
</div>
</template>

<script>


export default {
name: 'App',
components: {

}
}
</script>

<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>

封装

基本样式

vue2版本

npm install node-sass@7.0.1

npm install sass-loader@7.0.3

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
<style lang="scss">
.xy-button{
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
background: #ffffff;
border: 1px solid #dcdfe6;
color: #606266;
-webkit-appearance: none;
text-align: center;
box-sizing: border-box;
outline: none;
margin: 0;
transition: 0.1s;
font-weight: 500;
//禁止元素的文字被选中
-moz-user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
&:hover,
&:hover{
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
}
</style>

appearance 属性允许您使元素看上去像标准的用户界面元素。 https://blog.51cto.com/jiqing9006/3286196

Type

1
2
3
4
5
6
7
8
<div id="app">
<xy-button type="primary">primary按钮</xy-button>
<xy-button type="success">success按钮</xy-button>
<xy-button type="info">info按钮</xy-button>
<xy-button type="danger">danger按钮</xy-button>
<xy-button type="warning">warning按钮</xy-button>
<xy-button>default按钮</xy-button>
</div>
1
2
3
4
5
<template>
<button class="xy-button" :class="`xy-button-${type}`">
<slot/>
</button>
</template>
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
<style lang="scss"  scoped>
.xy-button-primary{
color:#fff;
background-color: #409eff;
border-color: #409eff;
&:hover,
&:focus{
background: #66b1ff;
background-color: #66b1ff;
color: #fff;
}
}
.xy-button-success{
color:#fff;
background-color: #67c23a;
border-color: #67c23a;
&:hover,
&:focus{
background: #85ce61;
background-color: #85ce61;
color: #fff;
}
}
.xy-button-info{
color:#fff;
background-color: #909399;
border-color: #909399;
&:hover,
&:focus{
background: #a6a9ad;
background-color: #a6a9ad;
color: #fff;
}
}
.xy-button-warning{
color:#fff;
background-color: #e6a23c;
border-color: #e6a23c;
&:hover,
&:focus{
background: #ebb563;
background-color: #ebb563;
color: #fff;
}
}
.xy-button-danger{
color:#fff;
background-color: #f56c6c;
border-color: #f56c6c;
&:hover,
&:focus{
background: #f78989;
background-color: #f78989;
color: #fff;
}
}
</style>

Plain

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<button
class="xy-button"
:class="[
`xy-button-${type}`,
{
'is-plain': plain,
},
]"
>
<slot />
</button>


props: {
type: {
type: String,
default: "default",
},
plain: {
type: Boolean,
default: false,
},
},


<style lang="scss" scoped>
.xy-button {
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
background: #ffffff;
border: 1px solid #dcdfe6;
color: #606266;
-webkit-appearance: nxy;
text-align: center;
box-sizing: border-box;
outline: nxy;
margin: 0;
transition: 0.1s;
font-weight: 500;
//禁止元素的文字被选中
-moz-user-select: nxy;
-webkit-user-select: nxy;
-moz-user-select: nxy;
-ms-user-select: nxy;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
&:hover,
&:hover {
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
&.is-plain {
&:hover,
&:focus {
background: #fff;
border-color: #489eff;
color: #409eff;
}
}
}
.xy-button-primary {
color: #fff;
background-color: #409eff;
border-color: #409eff;
&:hover,
&:focus {
background: #66b1ff;
background-color: #66b1ff;
color: #fff;
}
&.is-plain {
color: #409eff;
background: #ecf5ff;
&:hover,
&:focus {
background: #409eff;
border-color: #409eff;
color: #fff;
}
}
}
.xy-button-success {
color: #fff;
background-color: #67c23a;
border-color: #67c23a;
&:hover,
&:focus {
background: #85ce61;
background-color: #85ce61;
color: #fff;
}
&.is-plain {
color: #67c23a;
background: #c2e7b0;
&:hover,
&:focus {
background: #67c23a;
border-color: #67c23a;
color: #fff;
}
}
}

.xy-button-info {
color: #fff;
background-color: #909399;
border-color: #909399;
&:hover,
&:focus {
background: #a6a9ad;
background-color: #a6a9ad;
color: #fff;
}
&.is-plain {
color: #909399;
background: #d3d4d6;
&:hover,
&:focus {
background: #909399;
border-color: #909399;
color: #fff;
}
}
}
.xy-button-warning {
color: #fff;
background-color: #e6a23c;
border-color: #e6a23c;
&:hover,
&:focus {
background: #ebb563;
background-color: #ebb563;
color: #fff;
}
&.is-plain {
color: #e6a23c;
background: #f5dab1;
&:hover,
&:focus {
background: #e6a23c;
border-color: #e6a23c;
color: #fff;
}
}
}
.xy-button-danger {
color: #fff;
background-color: #f56c6c;
border-color: #f56c6c;
&:hover,
&:focus {
background: #f78989;
background-color: #f78989;
color: #fff;
}
&.is-plain {
color: #f56c6c;
background: #fbc4c4;
&:hover,
&:focus {
background: #f56c6c;
border-color: #f56c6c;
color: #fff;
}
}
}
</style>

图标

去阿里巴巴矢量图标库新建项目下载图标。

assets/fonts 下放入下载的文件

修改为自己项目类名

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
@font-face {
font-family: "iconfont"; /* Project id 4088014 */
src: url('iconfont.woff2?t=1685068317755') format('woff2'),
url('iconfont.woff?t=1685068317755') format('woff'),
url('iconfont.ttf?t=1685068317755') format('truetype'),
url('iconfont.svg?t=1685068317755#iconfont') format('svg');
}

[class*='xy-icon'] {
font-family: "iconfont" !important;
font-size: 16px;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

.xy-icon-add:before {
content: "\e664";
}

.xy-icon-bottom:before {
content: "\e666";
}

.xy-icon-close:before {
content: "\e668";
}

.xy-icon-direction-right:before {
content: "\e66c";
}

.xy-icon-direction-up:before {
content: "\e66d";
}


1
2
<xy-button  type="success" icon="add"></xy-button>
<xy-button type="info" icon="direction-up"></xy-button>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<button
class="xy-button"
:class="[
`xy-button-${type}`,
{
'is-plain': plain,
},
]"
>
<i v-if="icon" :class="`xy-icon-${icon}`"></i>
<!-- 如果没传入文本插槽,则不显示span内容 -->
<span v-if="$slots.default"><slot></slot></span>
</button>

icon: {
type: String,
default: ''
}

.xy-button [class*=one-icon-]+span{
margin-left: 5px;
}

[class*=’xy-icon’] css属性选择器 https://www.w3school.com.cn/css/css_selector_attribute.asp

[class*=one-icon-]+span 选择跟在one-icon- 后的首个span

https://www.w3school.com.cn/cssref/selector_element_plus.asp

点击事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<button class="xy-button" :class="[`xy-button-${type}`,{
'is-plain':plain,
'is-round':round,
'is-circle':circle,
}]"
@click="handleClick"
>
<i v-if="icon" :class="`xy-icon-${icon}`"></i>
<!-- 如果没传入文本插槽,则不显示span内容 -->
<span v-if="$slots.default"><slot></slot></span>
</button>



methods: {
handleClick (e) {
this.$emit('click', e)
}
}

1
2
3
4
5
6
7
8
<xy-button @click="handleButtonClick">按钮</xy-button>

methods: {
handleButtonClick () {
console.log('点击了Button')//获取信息!!
}
}

disabled

1
2
3
4
5
6
7
8
9
10
11
<button class="xy-button" :class="[`xy-button-${type}`,{
'is-plain':plain,
'is-disabled':disabled
}]"
@click="handleClick"
:disabled="disabled"
>
<i v-if="icon" :class="`xy-icon-${icon}`"></i>
<span v-if="$slots.default"><slot></slot></span>
</button>

其余的圆角等属性可自行添加

完整代码

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<template>
<button class="xy-button" :class="[`xy-button-${type}`,{
'is-plain':plain,
'is-disabled':disabled
}]"
@click="handleClick"
:disabled="disabled"
>
<i v-if="icon" :class="`xy-icon-${icon}`"></i>
<span v-if="$slots.default"><slot></slot></span>
</button>
</template>

<script>
export default {
name: "XyButton",
props: {
type: {
type: String,
default: "default",
},
plain: {
type: Boolean,
default: false,
},
icon: {
type: String,
default: ''
},
disabled: {
type: Boolean,
default: false
}
},

compxynts: {},
data() {
return {};
},
computed: {},
methods: {
handleClick (e) {
this.$emit('click', e)
}
}
};
</script>

<style lang="scss" scoped>
.xy-button {
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
background: #ffffff;
border: 1px solid #dcdfe6;
color: #606266;
-webkit-appearance: nxy;
text-align: center;
box-sizing: border-box;
outline: nxy;
margin: 0;
transition: 0.1s;
font-weight: 500;
//禁止元素的文字被选中
-moz-user-select: nxy;
-webkit-user-select: nxy;
-moz-user-select: nxy;
-ms-user-select: nxy;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
&:hover,
&:hover {
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
&.is-plain {
&:hover,
&:focus {
background: #fff;
border-color: #489eff;
color: #409eff;
}
}
}
.xy-button-primary {
color: #fff;
background-color: #409eff;
border-color: #409eff;
&:hover,
&:focus {
background: #66b1ff;
background-color: #66b1ff;
color: #fff;
}
&.is-plain {
color: #409eff;
background: #ecf5ff;
&:hover,
&:focus {
background: #409eff;
border-color: #409eff;
color: #fff;
}
}
}
.xy-button-success {
color: #fff;
background-color: #67c23a;
border-color: #67c23a;
&:hover,
&:focus {
background: #85ce61;
background-color: #85ce61;
color: #fff;
}
&.is-plain {
color: #67c23a;
background: #c2e7b0;
&:hover,
&:focus {
background: #67c23a;
border-color: #67c23a;
color: #fff;
}
}
}

.xy-button-info {
color: #fff;
background-color: #909399;
border-color: #909399;
&:hover,
&:focus {
background: #a6a9ad;
background-color: #a6a9ad;
color: #fff;
}
&.is-plain {
color: #909399;
background: #d3d4d6;
&:hover,
&:focus {
background: #909399;
border-color: #909399;
color: #fff;
}
}
}
.xy-button-warning {
color: #fff;
background-color: #e6a23c;
border-color: #e6a23c;
&:hover,
&:focus {
background: #ebb563;
background-color: #ebb563;
color: #fff;
}
&.is-plain {
color: #e6a23c;
background: #f5dab1;
&:hover,
&:focus {
background: #e6a23c;
border-color: #e6a23c;
color: #fff;
}
}
}
.xy-button-danger {
color: #fff;
background-color: #f56c6c;
border-color: #f56c6c;
&:hover,
&:focus {
background: #f78989;
background-color: #f78989;
color: #fff;
}
&.is-plain {
color: #f56c6c;
background: #fbc4c4;
&:hover,
&:focus {
background: #f56c6c;
border-color: #f56c6c;
color: #fff;
}
}
}
.xy-button [class*=one-icon-]+span{
margin-left: 5px;
}
.xy-button.is-disabled{
cursor: no-drop;
}
</style>

样式重复太多了 可以使用mixin来简化一下

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<style lang="scss" scoped>
.xy-button {
display: inline-block;
line-height: 1;
white-space: nowrap;
cursor: pointer;
background: #ffffff;
border: 1px solid #dcdfe6;
color: #606266;
-webkit-appearance: nxy;
text-align: center;
box-sizing: border-box;
outline: nxy;
margin: 0;
transition: 0.1s;
font-weight: 500;
//禁止元素的文字被选中
-moz-user-select: nxy;
-webkit-user-select: nxy;
-moz-user-select: nxy;
-ms-user-select: nxy;
padding: 12px 20px;
font-size: 14px;
border-radius: 4px;
&:hover,
&:hover {
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
&.is-plain {
&:hover,
&:focus {
background: #fff;
border-color: #489eff;
color: #409eff;
}
}
}
@mixin xy-button(
$color: #fff,
$bgc: #409eff,
$borColor: #409eff,
$hoverBackground: #66b1ff,
$hoverBgc: #66b1ff,
$hoverColor: #fff,
$plainColor: #409eff,
$plainBgc: #ecf5ff,
$plainHoverBackGround: #409eff,
$plainHoverBgc: #409eff,
$plainHoverColor: #fff
) {
color: $color;
background-color: $bgc;
border-color: $borColor;
&:hover,
&:focus {
background: $hoverBackground;
background-color: $hoverBgc;
color: $hoverColor;
}
&.is-plain {
color: $plainColor;
background: $plainBgc;
&:hover,
&:focus {
background: $plainHoverBackGround;
border-color: $plainHoverBgc;
color: $plainHoverColor;
}
}
}
.xy-button-primary {
@include xy-button();
}
.xy-button-success {
@include xy-button(
#fff,
#67c23a,
#67c23a,
#85ce61,
#85ce61,
#fff,
#67c23a,
#c2e7b0,
#67c23a,
#67c23a,
#fff
);
}

.xy-button-info {
color: #fff;
background-color: #909399;
border-color: #909399;
&:hover,
&:focus {
background: #a6a9ad;
background-color: #a6a9ad;
color: #fff;
}
&.is-plain {
color: #909399;
background: #d3d4d6;
&:hover,
&:focus {
background: #909399;
border-color: #909399;
color: #fff;
}
}
}
.xy-button-warning {
color: #fff;
background-color: #e6a23c;
border-color: #e6a23c;
&:hover,
&:focus {
background: #ebb563;
background-color: #ebb563;
color: #fff;
}
&.is-plain {
color: #e6a23c;
background: #f5dab1;
&:hover,
&:focus {
background: #e6a23c;
border-color: #e6a23c;
color: #fff;
}
}
}
.xy-button-danger {
color: #fff;
background-color: #f56c6c;
border-color: #f56c6c;
&:hover,
&:focus {
background: #f78989;
background-color: #f78989;
color: #fff;
}
&.is-plain {
color: #f56c6c;
background: #fbc4c4;
&:hover,
&:focus {
background: #f56c6c;
border-color: #f56c6c;
color: #fff;
}
}
}
.xy-button [class*="one-icon-"] + span {
margin-left: 5px;
}
.xy-button.is-disabled {
cursor: no-drop;
}
</style>