最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue組件封裝之dialog對(duì)話(huà)框組件詳解

 更新時(shí)間:2024年05月24日 09:12:49   作者:黑貓了個(gè)怪怪  
這篇文章主要介紹了Vue組件封裝之dialog對(duì)話(huà)框組件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、基礎(chǔ)準(zhǔn)備工作

1、創(chuàng)建一個(gè)基礎(chǔ)的vue項(xiàng)目包

2、創(chuàng)建components文件夾,用于存放組件,新建dialog.vue組件,可以自己取個(gè)名字

<script>
export default {
  name: "CatDialog",
};
</script>

3、在main.js中引入組件

import CatDialog from "./components/dialog.vue";
Vue.component(CatDialog.name, CatDialog);

4、App.vue中使用組件

二、dialog組件結(jié)構(gòu)搭建

dialog對(duì)話(huà)框,整體有一個(gè)動(dòng)畫(huà)效果,vue的動(dòng)畫(huà)效果,使用transition包裹需要?jiǎng)赢?huà)展示的元素,那么這個(gè)元素在顯示/隱藏時(shí)自動(dòng)添加一些類(lèi)名,此例詳見(jiàn)后續(xù)代碼

對(duì)話(huà)框分為三部分:

1、頭部:左側(cè)為標(biāo)題,使用具名插槽 title 占位,右側(cè)為按鈕/圖標(biāo)(關(guān)閉)

2、主體內(nèi)容,使用不具名的插槽占位

3、底部:一般都是一些操作,使用具名插槽 footer 占位,通常內(nèi)容是取消/確認(rèn)按鈕

需要傳入的參數(shù):

  • title:頭部標(biāo)題
  • width:對(duì)話(huà)框?qū)挾?/li>
  • top:對(duì)話(huà)框距離頂部的距離
  • visible:對(duì)話(huà)框的顯示/隱藏

頁(yè)面效果:

dialog.vue 具體代碼如下,style樣式太多,不逐一列出了,可根據(jù)需求自己定義:

<template>
  <transition name="dialog-fade">
    <!--self:事件修飾符,只有點(diǎn)擊自己才觸發(fā),點(diǎn)擊子元素不觸發(fā)  -->
    <div class="cat-dialog__wrapper" v-show="visible" @click.self="handleClose">
      <!-- 對(duì)話(huà)框 -->
      <div class="cat-dialog" :style="{ width, marginTop: top }">
 
        <!-- 對(duì)話(huà)框頂部 標(biāo)題 + 關(guān)閉按鈕 -->
        <div class="cat-dialog__header">
          <slot name="title">
            <span class="cat-dialog__title">{{ title }}</span>
          </slot>
          <button class="cat-dialog__headerbtn" @click="handleClose">
            <i class="cat-icon-close"></i>
          </button>
        </div>
 
        <!-- 對(duì)話(huà)框內(nèi)容 -->
        <div class="cat-dialog__body">
          <slot></slot>
        </div>
 
        <!-- 對(duì)話(huà)框底部 一般都是一些操作,沒(méi)有傳入footer插槽就不顯示v-if -->
        <div class="cat-dialog__footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  name: "CatDialog",
  props: {
    title: {
      type: String,
      default: "提示",
    },
    // 彈框?qū)挾?
    width: {
      type: String,
      default: "30%",
    },
    // 彈框距離頂部距離
    top: {
      type: String,
      default: "15vh",
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    handleClose() {
      //visible是父組件傳過(guò)來(lái)的props,子組件不能直接修改,需要子傳父
      this.$emit("update:visible", false);
    },
  },
};
</script>

transition動(dòng)畫(huà)代碼:

<style lang="scss">
// 進(jìn)入動(dòng)畫(huà)
.dialog-fade-enter-active {
  animation: dialog-fade-in 0.4s;
}
// 離開(kāi)動(dòng)畫(huà)
.dialog-fade-leave-active {
  animation: dialog-fade-out 0.4s;
}
 
@keyframes dialog-fade-in {
  0% {
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
  100% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}
 
@keyframes dialog-fade-out {
  0% {
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
  100% {
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
}
</style>

App.vue使用dialog組件:

<template>
  <div>
    <Cat-button type="primary" @click="visible = true">點(diǎn)擊彈出</Cat-button>
    <!-- 
      sync:事件修飾符,是一個(gè)語(yǔ)法糖寫(xiě)法,實(shí)現(xiàn)子組件修改父組件傳入的props
      父:visible.sync="visible"
      子:this.$emit("update:visible", false) 
    -->
    <Cat-dialog width="25%" top="100px" :visible.sync="visible">
      <template v-slot:title>
        <h3>標(biāo)題</h3>
      </template>
      <template>
        <p>主體內(nèi)容,隨便寫(xiě)點(diǎn)什么...</p>
        <input type="text" placeholder="請(qǐng)輸入信息" />
      </template>
      <template v-slot:footer>
        <Cat-button @click="visible = false">取消</Cat-button>
        <Cat-button type="primary" @click="visible = false">確定</Cat-button>
      </template>
    </Cat-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
    };
  },
};
</script>
<style lang="scss" scoped>
.cat-button {
  margin-right: 10px !important;
}
</style>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

湖南省| 云阳县| 沐川县| 黄大仙区| 十堰市| 东至县| 灵宝市| 裕民县| 新野县| 太湖县| 防城港市| 秦皇岛市| 鲁山县| 遂溪县| 登封市| 大英县| 都匀市| 西林县| 西林县| 苏尼特右旗| 克拉玛依市| 福贡县| 德安县| 阿尔山市| 札达县| 滦南县| 福鼎市| 克什克腾旗| 牙克石市| 五家渠市| 平泉县| 城步| 邛崃市| 宜兰市| 扶绥县| 东明县| 固始县| 揭东县| 贞丰县| 永泰县| 寿阳县|