Vue組件封裝之dialog對(duì)話框組件詳解
一、基礎(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ì)話框,整體有一個(gè)動(dòng)畫效果,vue的動(dòng)畫效果,使用transition包裹需要?jiǎng)赢嬚故镜脑兀敲催@個(gè)元素在顯示/隱藏時(shí)自動(dòng)添加一些類名,此例詳見后續(xù)代碼
對(duì)話框分為三部分:
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ì)話框?qū)挾?/li>
- top:對(duì)話框距離頂部的距離
- visible:對(duì)話框的顯示/隱藏
頁面效果:
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ì)話框 --> <div class="cat-dialog" :style="{ width, marginTop: top }"> <!-- 對(duì)話框頂部 標(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ì)話框內(nèi)容 --> <div class="cat-dialog__body"> <slot></slot> </div> <!-- 對(duì)話框底部 一般都是一些操作,沒有傳入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動(dòng)畫代碼:
<style lang="scss"> // 進(jìn)入動(dòng)畫 .dialog-fade-enter-active { animation: dialog-fade-in 0.4s; } // 離開動(dòng)畫 .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è)語法糖寫法,實(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)容,隨便寫點(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)文章
一文詳解Vue3中簡(jiǎn)單diff算法的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹Vue3中簡(jiǎn)單diff算法的實(shí)現(xiàn)與使用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-09-09Vue + OpenLayers 快速入門學(xué)習(xí)教程
大家都知道使用 Openlayers可以很靈活自由的做出各種地圖和空間數(shù)據(jù)的展示。而且這個(gè)框架是完全免費(fèi)和開源的,本文記錄下 Vue 使用 OpenLayers 入門,使用 OpenLayers 創(chuàng)建地圖組件的相關(guān)知識(shí),需要的朋友一起學(xué)習(xí)下吧2021-09-09vue3中通過遍歷傳入組件名稱動(dòng)態(tài)創(chuàng)建多個(gè)component 組件
這篇文章主要介紹了vue3中通過遍歷傳入組件名稱動(dòng)態(tài)創(chuàng)建多個(gè)component 組件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03一文詳解Vue如何整合Echarts實(shí)現(xiàn)可視化界面
ECharts,縮寫來自Enterprise Charts,商業(yè)級(jí)數(shù)據(jù)圖表,一個(gè)純Javascript的圖表庫,可以流暢的運(yùn)行在PC和移動(dòng)設(shè)備上。本文將在Vue中整合Echarts實(shí)現(xiàn)可視化界面,感興趣的可以了解一下2022-04-04Vue3中實(shí)現(xiàn)拖拽和縮放自定義看板 vue-grid-layout的方法
這篇文章主要介紹了Vue3中實(shí)現(xiàn)拖拽和縮放自定義看板 vue-grid-layout的方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03el-input限制輸入正整數(shù)的兩種實(shí)現(xiàn)方式
el-input框是Element UI庫中的一個(gè)輸入框組件,用于接收用戶的輸入,這篇文章主要介紹了el-input限制輸入正整數(shù),需要的朋友可以參考下2024-02-02