Vue彈窗組件的實現(xiàn)方法
本文實例為大家分享了Vue彈窗組件的實現(xiàn)具體代碼,供大家參考,具體內(nèi)容如下
彈窗組件包含內(nèi)容:
- 彈窗遮罩層
- 內(nèi)容層的實現(xiàn)(涉及slot、props、
$on
、$emit
)
實現(xiàn)步驟:
1、搭建組件UI樣式,HTML、css實現(xiàn)遮罩層、內(nèi)容區(qū)
2、編寫彈窗內(nèi)容:通過組件slot插槽接收父組件傳遞過來的彈窗內(nèi)容
3、組件開關(guān)的實現(xiàn):通過父組件傳遞進來的props控制組件的顯示與隱藏,子組件關(guān)閉時通過事件 $emit 觸發(fā)父組件改變狀態(tài)值。
組件代碼實現(xiàn)
<template> ?? ?<div class="modal-bg" v-show="show"> ?? ??? ?<div class="modal-container"> ?? ??? ??? ?<div class="modal-header"> ?? ??? ??? ??? ?{{title}} ?? ??? ??? ?</div> ?? ??? ??? ?<div class="modal-main"> ?? ??? ??? ??? ?<!--? ?? ??? ??? ??? ??? ?如果有多個插槽時,可以采用具名插槽的方式實現(xiàn) ?? ??? ??? ??? ??? ?<slot name="main"></slot> ?? ??? ??? ??? ? --> ?? ??? ??? ??? ?<slot></slot> ?? ??? ??? ?</div> ?? ??? ??? ?<div class="modal-footer"> ?? ??? ??? ??? ?<button @click="close">關(guān) 閉</button> ?? ??? ??? ??? ?<button @click="submit">確 定</button> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </template> <script> export default{ ?? ?name: 'modal', ?? ?data() { ?? ??? ?return {}?? ? ?? ?}, ?? ?props: { ?? ??? ?show: { ? ?// 控制彈窗展示 ?? ??? ??? ?type: Boolean, ?? ??? ??? ?default: false,?? ? ?? ??? ??? ?required: true, ? // 必傳遞 ?? ??? ?}, ?? ??? ?title: { ?? ??? ??? ?type: String, ?? ??? ??? ?default: 'title', ?? ??? ?} ?? ?}, ?? ?methods: { ?? ??? ?// 通過事件綁定及$emit來執(zhí)行父組件的方法,改變彈窗展示狀態(tài) ?? ??? ?close() { ?? ??? ??? ?this.$emit("hideModal");?? ? ?? ??? ?}, ?? ??? ?submit() { ?? ??? ??? ?this.$emit("submit");?? ? ?? ??? ?} ?? ?} } </script> <style> .modal-bg{ ?? ?position: fixed; ?? ?top: 0; ?? ?right: 0; ?? ?bottom: 0; ?? ?left: 0; ?? ?display: flex; ?? ?justify-content: center; ?? ?align-items: center; ?? ?background: rgba(0, 0, 0, .4); ?? ?z-index:999; } .modal-container{ ?? ?border-radius: 8px; ?? ?background: #fff; } .model-header{ ?? ?heigth: 60px; ?? ?background: blue; ?? ?color:#fff; } .modal-main { ?? ?padding: 20px 40px; } .modal-footer{ ?? ?height: 60px; ?? ?display: flex; ?? ?justify-content: center; ?? ?align-item: center; ?? ?border-top: 1px solid #000; } .modal-footer button{ ?? ?width:100px; } </style>
父組件中調(diào)用:
<template> ?? ?<div> ?? ??? ?<Modal :show="show" :title="title" @hideModal="hideModal" @submit="submit"> ?? ??? ??? ?<!--? ?? ??? ??? ??? ?向 slot 插槽中展示的內(nèi)容? ?? ??? ??? ??? ?具名傳遞: <div slot="main">我是具名插槽傳遞內(nèi)容的</div> ?? ??? ??? ?--> ?? ??? ??? ?<div>我是slot中的內(nèi)容</div> ?? ??? ?</Modal> ?? ?</div> </template> <script> import Modal from "./modal.vue"; export default { ?? ?name: "parentVue", ?? ?data() { ?? ??? ?return { ?? ??? ??? ?show: false, ?? ??? ??? ?title: "我是彈窗標(biāo)題", ?? ??? ?}?? ? ?? ?}, ?? ?components: { ?? ??? ?Modal, ?? ?}, ?? ?methods: { ?? ??? ?// 關(guān)閉彈窗 ?? ??? ?hideModal() { ?? ??? ??? ?this.show = false; ?? ??? ?} ?? ??? ?// 顯示彈窗 ?? ??? ?submit() { ?? ??? ??? ?this.show = true;?? ? ?? ??? ?} ?? ?} } </script> <style> </style>
溫馨提示:
目前的市面上,有很多現(xiàn)有的 UI 組件庫,如Element-UI等,很少會直接編寫UI樣式代碼之類的操作,可以直接使用第三方庫完成項目需求。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用vue的雙向綁定簡單實現(xiàn)一個todo-list的示例代碼
本篇文章主要介紹了用vue的雙向綁定簡單實現(xiàn)一個todo的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08VueX學(xué)習(xí)之modules和namespacedVueX詳細教程
這篇文章主要為大家介紹了VueX學(xué)習(xí)之modules和namespacedVueX詳細教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06vue3內(nèi)嵌iframe的傳參與接收參數(shù)代碼示例
這篇文章主要給大家介紹了關(guān)于vue3內(nèi)嵌iframe的傳參與接收參數(shù)的相關(guān)資料,Vue項目中使用iframe及傳值功能相信有不少人都遇到過,需要的朋友可以參考下2023-07-07vue自定義加載指令v-loading占位圖指令v-showimg
這篇文章主要為大家介紹了vue自定義加載指令和v-loading占位圖指令v-showimg的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08elementui?Select選擇器嵌套tree實現(xiàn)TreeSelect方式
這篇文章主要介紹了elementui?Select選擇器嵌套tree實現(xiàn)TreeSelect方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10