vue+vuetify构建简单消息确认框
程序员文章站
2022-05-31 14:38:38
...
vue框架自己好像没有弹出框,而vuetify有弹出框v-dialog,没有确认框confirm,虽然确认框本身就是弹出框,但是弹出框的功能有个特点,就是确定做一件事情,而取消了就是做另一件事情,类似一个Promise要做的事情。
今天主要就是要自定义一个确认框,我们利用组件的思维,先定义组件结构,然后定义组件如何配合文档document工作,最后声明和调用。按照这个思路,我们分如下几步:
1、定义组件结构体src/components/Confirm.vue
<template>
<div class="dialogwrapper" v-if="show">
<div class="overlay"></div>
<v-card class="dialog">
<v-card-title>提示</v-card-title>
<v-card-text>
您确定这么做吗?
</v-card-text>
<v-card-actions>
<v-btn @click="ok">确定</v-btn>
<v-btn @click="cancel">取消</v-btn>
</v-card-actions>
</v-card>
</div>
</template>
<script>
export default {
data() {
return {
promiseStatus: null,
show: false
}
},
methods: {
confirm() {
let _this = this;
this.show = true;
return new Promise(function (resolve,reject){
_this.promiseStatus = {resolve,reject};
});
},
ok(){
this.show = false;
this.promiseStatus && this.promiseStatus.resolve();
},
cancel(){
this.show = false;
this.promiseStatus && this.promiseStatus.reject();
}
}
}
</script>
<style>
.dialogwrapper{
align-items: center;
display: flex;
height: 100%;
justify-content: center;
left: 0px;
pointer-events: none;
position: fixed;
top: 0px;
width: 100%;
z-index: 6;
transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1) 0s, z-index 1ms ease 0s;
outline: none;
}
.dialog{
overflow-y: auto;
pointer-events: auto;
width: 100%;
z-index: inherit;
box-shadow: rgba(0, 0, 0, 0.2) 0px 11px 15px -7px, rgba(0, 0, 0, 0.14) 0px 24px 38px 3px, rgba(0, 0, 0, 0.12) 0px 9px 46px 8px;
border-radius: 4px;
margin: 24px;
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1) 0s;
max-width:290px;
}
.overlay{
align-items: center;
border-radius: inherit;
display: flex;
justify-content: center;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
pointer-events: auto;
background:#000;
opacity:0.46;
}
</style>
2、定义插件src/plugins/confirm.js
import Vue from 'vue'
import Confirm from '@/components/Confirm.vue'
const VueComponent = Vue.extend(Confirm)
const vm = new VueComponent().$mount()
let init = false;
let defaultOptions = {
}
const confirm = function(options){
Object.assign(vm,defaultOptions,options,{
type:"confirm"
})
if(!init){
document.body.appendChild(vm.$el)
init = true;
}
return vm.confirm();
};
export default confirm;
3、全局声明组件src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import vuetify from '@/plugins/vuetify'
import confirm from '@/plugins/confirm'
Vue.config.productionTip = false
Vue.prototype.$confirm = confirm
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
vuetify,
components: { App },
template: '<App/>'
})
4、调用src/components/TestPage.vue
<template>
<v-card>
<v-btn @click="confirm">open</v-btn>
</v-card>
</template>
<script>
export default {
methods: {
confirm(){
this.$confirm({
}).then(()=>{
console.log("ok");
}).catch(()=>{
console.log("cancel");
})
}
}
}
</script>
5、效果图:
以上是一个简单的示例,我们在需要使用的地方直接通过this.$confirm().then().catch()就可以把逻辑全部设置好,可以看出this.$confirm是一个Promise,而这个Promise是通过定义组件结构的时候confirm方法返回的。如下所示:
confirm() {
let _this = this;
this.show = true;
return new Promise(function (resolve,reject){
_this.promiseStatus = {resolve,reject};
});
}
我们在src/plugins/confirm.js中通过了一些方法找到了组件挂载点,组件元素,组件中的方法。
这个组件不比vuetify自带的v-dialog一开始就是长在文档中的,而是需要我们手动appendChild的方式将元素插入文档中。
推荐阅读
-
javascript实现对话框功能警告(alert 消息对话框)确认(confirm 消息对话框)
-
JS简单小应用之关于确认是否关闭框和页面跳转的写法
-
JavaScript-确认(confirm 消息对话框)
-
javascript实现对话框功能警告(alert 消息对话框)确认(confirm 消息对话框)
-
thinkPHP删除前弹出确认框的简单实现方法_PHP
-
thinkPHP删除前弹出确认框的简单实现方法_php实例
-
vue+vuetify构建简单消息确认框
-
mui弹出框——输入确认消息框
-
memcache构建简单的内存消息队列_PHP教程
-
thinkPHP删除前弹出确认框的简单实现方法,thinkphp弹出_PHP教程