Vue組件封裝之dialog對話框組件詳解
一、基礎(chǔ)準(zhǔn)備工作
1、創(chuàng)建一個基礎(chǔ)的vue項目包
2、創(chuàng)建components文件夾,用于存放組件,新建dialog.vue組件,可以自己取個名字
<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對話框,整體有一個動畫效果,vue的動畫效果,使用transition包裹需要動畫展示的元素,那么這個元素在顯示/隱藏時自動添加一些類名,此例詳見后續(xù)代碼
對話框分為三部分:
1、頭部:左側(cè)為標(biāo)題,使用具名插槽 title 占位,右側(cè)為按鈕/圖標(biāo)(關(guān)閉)
2、主體內(nèi)容,使用不具名的插槽占位
3、底部:一般都是一些操作,使用具名插槽 footer 占位,通常內(nèi)容是取消/確認(rèn)按鈕
需要傳入的參數(shù):
- title:頭部標(biāo)題
- width:對話框?qū)挾?/li>
- top:對話框距離頂部的距離
- visible:對話框的顯示/隱藏
頁面效果:

dialog.vue 具體代碼如下,style樣式太多,不逐一列出了,可根據(jù)需求自己定義:
<template>
<transition name="dialog-fade">
<!--self:事件修飾符,只有點擊自己才觸發(fā),點擊子元素不觸發(fā) -->
<div class="cat-dialog__wrapper" v-show="visible" @click.self="handleClose">
<!-- 對話框 -->
<div class="cat-dialog" :style="{ width, marginTop: top }">
<!-- 對話框頂部 標(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>
<!-- 對話框內(nèi)容 -->
<div class="cat-dialog__body">
<slot></slot>
</div>
<!-- 對話框底部 一般都是一些操作,沒有傳入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是父組件傳過來的props,子組件不能直接修改,需要子傳父
this.$emit("update:visible", false);
},
},
};
</script>transition動畫代碼:
<style lang="scss">
// 進(jìn)入動畫
.dialog-fade-enter-active {
animation: dialog-fade-in 0.4s;
}
// 離開動畫
.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">點擊彈出</Cat-button>
<!--
sync:事件修飾符,是一個語法糖寫法,實現(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)容,隨便寫點什么...</p>
<input type="text" placeholder="請輸入信息" />
</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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue + OpenLayers 快速入門學(xué)習(xí)教程
大家都知道使用 Openlayers可以很靈活自由的做出各種地圖和空間數(shù)據(jù)的展示。而且這個框架是完全免費和開源的,本文記錄下 Vue 使用 OpenLayers 入門,使用 OpenLayers 創(chuàng)建地圖組件的相關(guān)知識,需要的朋友一起學(xué)習(xí)下吧2021-09-09
vue3中通過遍歷傳入組件名稱動態(tài)創(chuàng)建多個component 組件
這篇文章主要介紹了vue3中通過遍歷傳入組件名稱動態(tài)創(chuàng)建多個component 組件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
一文詳解Vue如何整合Echarts實現(xiàn)可視化界面
ECharts,縮寫來自Enterprise Charts,商業(yè)級數(shù)據(jù)圖表,一個純Javascript的圖表庫,可以流暢的運行在PC和移動設(shè)備上。本文將在Vue中整合Echarts實現(xiàn)可視化界面,感興趣的可以了解一下2022-04-04
Vue3中實現(xiàn)拖拽和縮放自定義看板 vue-grid-layout的方法
這篇文章主要介紹了Vue3中實現(xiàn)拖拽和縮放自定義看板 vue-grid-layout的方法,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
el-input限制輸入正整數(shù)的兩種實現(xiàn)方式
el-input框是Element UI庫中的一個輸入框組件,用于接收用戶的輸入,這篇文章主要介紹了el-input限制輸入正整數(shù),需要的朋友可以參考下2024-02-02

