1
需求:H5项目在退出登录时需要弹出弹框提示是否确认退出登录,但该项目体积较小,没有引入组件库,所以决定自己封装一个dialog弹框

image-20210924162623520

子组件

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
<template>
<div v-show="dialogVisible">
<div class="van-overlay" style="z-index: 2008;"></div>
<div role="dialog" class="van-dialog" style="z-index: 2009;">
<div class="van-dialog__content van-dialog__content--isolated">
<div class="van-dialog__message">{{title}}</div>
</div>
<div class="van-hairline-top van-dialog__footer">
<button class="van-button van-button--default van-button--large van-dialog__cancel" @click="submit('0')">
<div class="van-button__content">
<span class="van-button__text">{{tipA}}</span>
</div>
</button>
<button class="van-button van-button--default van-button--large van-dialog__confirm van-hairline-left" @click="submit('1')">
<div class="van-button__content">
<span class="van-button__text">{{tipB}}</span>
</div>
</button>
</div>
</div>
</div>

</template>

<script>
export default {
name: 'dialog',
props: {
dialogVisible: {
type: Boolean,
default: false,
},
title: {
type: String,
default: '',
},
tipA: {
type: String,
default: '取消',
},
tipB: {
type: String,
default: '确定',
},
},
data() {
return {}
},
created() {
},
methods: {
submit(item) {
this.$emit('dialogSubmit',item)
}
},
}
</script>

<style>
.van-button__content:before {
content: " ";
}
.van-button__content {
display: -webkit-box;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
height: 100%;
}
* {
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
}
.van-hairline, .van-hairline-bottom, .van-hairline-left, .van-hairline-right, .van-hairline-surround, .van-hairline-top {
position: relative;
}
.van-dialog__footer {
display: -webkit-box;
display: -webkit-flex;
display: flex;
overflow: hidden;
-webkit-user-select: none;
user-select: none;
}
.van-dialog__message {
-webkit-box-flex: 1;
-webkit-flex: 1;
flex: 1;
max-height: 60vh;
padding: 26px 24px;
overflow-y: auto;
font-size: 16px;
line-height: 20px;
white-space: pre-wrap;
text-align: center;
word-wrap: break-word;
-webkit-overflow-scrolling: touch;
}
.van-dialog__content--isolated {
display: -webkit-box;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
align-items: center;
min-height: 104px;
}
.van-dialog {
position: fixed;
top: 45%;
left: 50%;
width: 320px;
overflow: hidden;
font-size: 16px;
background-color: #fff;
border-radius: 16px;
-webkit-transform: translate3d(-50%,-50%,0);
transform: translate3d(-50%,-50%,0);
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: .3s;
transition: .3s;
-webkit-transition-property: opacity,-webkit-transform;
transition-property: opacity,-webkit-transform;
transition-property: transform,opacity;
transition-property: transform,opacity,-webkit-transform;
}
.van-overlay {
position: fixed;
top: 0;
left: 0;
z-index: 1;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,.7);
}
.van-button {
position: relative;
display: inline-block;
box-sizing: border-box;
height: 44px;
margin: 0;
padding: 0;
font-size: 16px;
line-height: 1.2;
text-align: center;
border-radius: 2px;
cursor: pointer;
-webkit-transition: opacity .2s;
transition: opacity .2s;
-webkit-appearance: none;
}
.van-button--default {
color: #323233;
background-color: #fff;
border: 1px solid #ebedf0;
}
.van-button--large {
width: 100%;
height: 50px;
}
.van-dialog__cancel, .van-dialog__confirm {
-webkit-box-flex: 1;
-webkit-flex: 1;
flex: 1;
height: 48px;
margin: 0;
border-right: 0;
}
.van-dialog__confirm, .van-dialog__confirm:active {
color: #ee0a24;
}
.van-button:before {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
height: 100%;
background-color: #000;
border: inherit;
border-color: #000;
border-radius: inherit;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
opacity: 0;
content: " ";
}
</style>

父组件引入

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
<template>
<myDialog :title="title" :tipA="tipA" :tipB="tipB" @dialogSubmit = "dialogSubmit" :dialogVisible = "dialogVisible">
</myDialog>
</template>
<script>
import myDialog from '../../common/util/myDialog.vue';
export default {
data(){
return {
tipB: '残忍离开',
tipA: '手误了~',
title: '确认登出账户吗?',
dialogVisible: false
}
},
components:{
myDialog
},
methods:{//所有方法集中定义
//登出确认
dialogSubmit(val) {
if(val === '1'){
this.dialogVisible = false;
doLogoutC({

}).then(resp=> {
window.location.href = "/rights/c/Mine"
}).catch(err => {
this.$toast.showToast(err);
})
}else{
this.dialogVisible = false;
}
},
logout() {
this.dialogVisible = true;
},
},
created(){//页面渲染前执行
//TODO
},
mounted() {//页面渲染后执行
}
}
</script>

<style>

</style>