如何实现一个vue的confirm函数组件

vue文件(由于项目中使用的vant所以button按钮偷懒了,可自行替换)

<template>
    <div  class="cofirm-bg" v-if="control">
      <div class="wrapper" @click.stop>
        <div class="block">
          <div class="close" @click="cancel">×</div>
          <div class="content">{{content.title}}</div>
          <div class="tips-btn">
            <van-button type="default" v-if="content.cancel"   @click="cancel" style="margin-right:34px" class="btn">{{content.cancel}}</van-button>
            <van-button type="info" v-if="content.confirm"   @click="confirm" class="btn">{{content.confirm}}</van-button>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
export default {
  data(){
    return {
      control :true,
      content:{
        title:'',
        cancel:'',
        confirm:''
      }
    }
  },
  methods:{
    confirm(){
      return true
    },
    cancel(){
      return false
    }
  }
};
</script>

<style scoped lang="scss">
.cofirm-bg{
  background: rgba(0,0,0,0.60);
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9999;
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
.close {
  margin:0 15px;
  margin-top: 10px;
  text-align: right;
  font-size: 20px;
  color: #CCCCCC;
}
.content {
  margin: 0 25px;
  margin-top: 15px;
  margin-bottom: 30px;
  font-size: 15px;
  color: #666666;
  letter-spacing: 0;
  line-height: 24px;
  text-align: left;
}
.tips-btn{
  margin-bottom: 30px;
  margin-bottom: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  .btn{
    padding: 0 22px ;
    height: 36px;
    line-height: 36px;
    border-radius: 22.5px;
  }
}
.block {
  box-sizing: border-box;
  width: 284px;
  border-radius: 4px;
  background-color: #fff;
}
</style>

js文件

import Vue from 'vue'
import confirm from './confirm.vue'

const confirmConstructor = Vue.extend(confirm)
let confirmChoose = function(content){
  return new Promise((res,rej)=>{
    let confirmEl = new confirmConstructor({
      el: document.createElement('div')
    })
    document.body.appendChild(confirmEl.$el)
    confirmEl.content = content
    confirmEl.confirm = function(){
      res(true)
      confirmEl.control = false
    }
    confirmEl.cancel = function(){
      rej(false)
      confirmEl.control = false
    }
  })

}
export default confirmChoose

在main.js中引用

import confirm from'./components/confirm'
Vue.prototype.$confirm = confirm;//挂载到Vue的prototype上
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容