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
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
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="fontFace.css">
<style>
[class*=" m-icon-"], [class^=m-icon-] {
font-family: element-icons!important;
speak: none;
font-style: normal;
font-weight: 400;
font-variant: normal;
text-transform: none;
line-height: 1;
vertical-align: baseline;
display: inline-block;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
/*基本样式*/
.m-alert {
width: 100%;
padding: 8px 16px;
margin: 0;
box-sizing: border-box;
border-radius: 4px;
position: relative;
background-color: #fff;
overflow: hidden;
color: #fff;
display: table;
transition: opacity .2s;
margin-top:10px;
border: 1px solid #ccc;
}
.m-content {
display: table-cell;
padding: 0 8px;
}
.m-message {
font-size: 13px;
line-height: 18px;
}
/*不同状态样式*/
.m-alert-success{
background-color:#13ce66;
}
.m-alert-info{
background-color:#50bfff;
}
.m-alert-warning{
background-color:#f7ba2a;
}
.m-alert-error{
background-color:#ff4949;
}
/*关闭按钮样式*/
.m-closebtn {
font-size: 12px;
color: #fff;
opacity: 1;
top: 12px;
right: 15px;
position: absolute;
cursor: pointer;
}
.m-icon-close:before {
content: "\E60C";
}
/*小图标样式*/
.m-icon {
font-size: 16px;
width: 16px;
display: table-cell;
color: #fff;
vertical-align: middle;
}
.m-icon-success:before {
content: "\E609";
}
.m-icon-warning:before {
content: "\E615";
}
.m-icon-info:before {
content: "\E615";
}

.m-icon-error:before {
content: "\E60B";
}
/*自定义图标*/
.m-icon-message:before {
content: "\E618";
}

.m-icon-menu:before {
content: "\E617";
}

.m-icon-setting:before {
content: "\E61E";
}

#app {
width: 500px;
}

</style>
<script src="vue.js"></script>
<script>

</script>
</head>

<body>
<div id="app">
<alert type="info" title="这里有一个消息要提示"></alert>
<alert type="success" title="成功的消息"></alert>
<alert type="error" title="错误的消息"></alert>
<alert type="warning" title="警告的消息" :closeable="false" :showicon="true" style="color:#333"></alert>
<alert @close-click="closed">
<template slot="messageTemp">
<p>你好张幸</p>
<p>你好张幸</p>
<p>你好张幸</p>
<p>你好张幸</p>
<p>你好张幸</p>
<p>你好张幸</p>
<i slot="iconTemp" class="m-icon m-icon-menu"></i>
</template>
</alert>
</div>
<script>
/*
alert提醒框有四种状态:
info success error warning

设置的props:
type 提醒框类型 默认为info
title 提示信息 '这里有一个消息要提示'
closeable 是否禁用关闭 默认为true
showicon 是否显示图标 默认为fasle
style 设置提醒框样式 默认为{}

定制模板:
slot为iconTmp 定制icon模板
slot为titleTmp 定制提示信息结构

监控状态变化:
事件名close-click 点击关闭X触发

*/
Vue.component("alert",{
props:{
type:{
type:String,
default:"success"
},
title:{
type:String,
default:"这是一个提醒的消息"
},
closeable:{
type:Boolean,
default:true
},
showicon:{
type:Boolean,
default:false
},
style:String
},
computed:{
classes:function(){
return `m-alert-${this.type}`
},
icon:function(){
return `m-icon-${this.type}`
}
},
template:`
<div class="m-alert" :class="[classes]" :style="style" >

<slot name="iconTemp">
<i class="m-icon" :class="[icon]" v-if="showicon"></i>

</slot>
<div class="m-content">
<slot name="messageTemp">
<span class="m-message">{{title}}</span>
</slot>
<i class="m-closebtn m-icon-close" v-if="closeable" @click="closeHander"></i>
</div>
</div>
`,
methods:{
closeHander(){
this.$emit("close-click")
}
}
})
new Vue({
el:"#app",
methods:{
closed(){
alert("关闭后执行的行为")
}
}
})
</script>
</body>
</html>