vue實現(xiàn)自定義dialog組件

不知道大家有沒有遇到這種情況。平時過于依賴組件庫,如果產(chǎn)品對某個組件有特殊的要求。我們與其去改,去覆蓋組件庫的源碼。還不如自己寫一個組件,畢竟是自己寫的,不管你想怎么改,都方便容易的很。這里就實現(xiàn)一個dialog組件和input組件,不基于任何組件庫。供大家參考

效果展示

在這里插入圖片描述
在這里插入圖片描述

MDialog.vue

<template>
  <transition name="fade">
    <div class="dialog-mask">
      <div class="dialog-wrapper animated fadeInUp" ref="dialogWrapper">
        <div class="dialog-container">
          <div class="dialog-header">{{title}}</div>
          <div class="dialog-body">
            <slot></slot>
          </div>
          <div class="dialog-footer">
            <div class="left i-block" @click="dialogClose">{{cancel}}</div>
            <div class="right i-block" @click="submit">{{confirm}}</div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
import '@/../public/css/animate.min.css';
@Component({
  components: {}
})
export default class extends Vue {
  @Prop()
  private title!: string;
  @Prop()
  private cancel!: string;
  @Prop()
  private confirm!: string;
  private dialogClose() {
    this.$emit("close");
  }
  private submit() {
    this.$emit("submit");
  }
}
</script>
<style scoped lang="scss">
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.4s;
}
.fade-enter,
.fade-leave-active {
  opacity: 0;
}
.dialog-mask {
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 99;
  display: table;
  animation-duration: 0.6s;
  .dialog-wrapper {
    display: table-cell;
    vertical-align: middle;
    animation-duration: 0.3s;
    .dialog-container {
      margin: auto;
      top: 50%;
      background-color: white;
      width: 260px;
      height: 150px;
      border-radius: 12px;
      .dialog-header {
        background-color: #f3fff7;
        color: #42b983;
        height: 32px;
        border-radius: 12px 12px 0 0;
        border-bottom: 1px solid #cccccc;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 13px;
        color: #38c26b;
      }
      .dialog-body {
        display: flex;
        justify-content: center;
        align-items: center;
        margin: 20px 0;
        height: 50px;
        font-size: 13px;
      }
      .dialog-footer {
        height: 40px;
        width: 100%;
        background-color: #ffffff;
        border-radius: 0 0 12px 12px;
        display: table;
        .left {
          width: 50%;
          height: 100%;
          vertical-align: middle;
          display: table-cell;
          font-size: 13px;
          color: #38c26b;
        }
        .right {
          width: 50%;
          height: 100%;
          vertical-align: middle;
          display: table-cell;
          font-size: 13px;
          color: #38c26b;
        }
        .left:hover {
          cursor: pointer;
        }
        .right:hover {
          cursor: pointer;
        }
      }
    }
  }
}
</style>

DialogInput.vue

<template>
  <div class="input-wrap">
    <input
      type="text"
      :value="value"
      :placeholder="placeholder"
      @input="$emit('input',$event.target.value)"
    />
  </div>
</template>
<script lang='ts'>
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
@Component({
  components: {}
})
export default class extends Vue {
  @Prop()
  private value!: string;
  @Prop()
  private placeholder!: string;
}
</script>
<style scoped lang="scss">
.input-wrap {
  border: 1px solid #56d16a;
  border-radius: 5px;
  height: 32px;
  width: 90%;
  background-color: #f3fff7;
  display: flex;
  align-items: center;
  justify-content: center;
  input {
    background-color: #f3fff7;
    font-size: 13px;
    height: 25px;
    width: 95%;
    border: 0px;
    text-indent: 3px;
  }
}
</style>

父組件中使用

<m-dialog
      @close="closeDialog"
      @submit="handleSubmit"
      v-if="visible"
      cancel="取消"
      confirm="確定"
      title="提示"
    >
      <dialog-input placeholder="請輸入手機號碼" v-model="value"></dialog-input>
      <!-- 您確定要取消該訂單嗎 -->
</m-dialog>

這里script部分是用ts寫的,你可以改成vue的語法。template和style部分不用改。
窗體部分使用了animate.css的動畫,直接下好css,在script導(dǎo)入就可以了。遮罩層使用了transition做fadein fadeout效果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一:什么是閉包?閉包的用處? (1)閉包就是能夠讀取其他函數(shù)內(nèi)部變量的函數(shù)。在本質(zhì)上,閉包就 是將函數(shù)內(nèi)部和函數(shù)外...
    xuguibin閱讀 10,031評論 1 52
  • 概要 64學(xué)時 3.5學(xué)分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,849評論 0 3
  • 本文章是我最近在公司的一場內(nèi)部分享的內(nèi)容。我有個習(xí)慣就是每次分享都會先將要分享的內(nèi)容寫成文章。所以這個文集也是用來...
    Awey閱讀 9,575評論 4 67
  • 基于Vue的一些資料 內(nèi)容 UI組件 開發(fā)框架 實用庫 服務(wù)端 輔助工具 應(yīng)用實例 Demo示例 element★...
    嘗了又嘗閱讀 1,284評論 0 1
  • UI組件 element- 餓了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的組件庫 m...
    小姜先森o0O閱讀 10,114評論 0 72

友情鏈接更多精彩內(nèi)容