Vue彈窗組件的實(shí)現(xiàn)方法
本文實(shí)例為大家分享了Vue彈窗組件的實(shí)現(xiàn)具體代碼,供大家參考,具體內(nèi)容如下
彈窗組件包含內(nèi)容:
- 彈窗遮罩層
- 內(nèi)容層的實(shí)現(xiàn)(涉及slot、props、
$on、$emit)
實(shí)現(xiàn)步驟:
1、搭建組件UI樣式,HTML、css實(shí)現(xiàn)遮罩層、內(nèi)容區(qū)
2、編寫(xiě)彈窗內(nèi)容:通過(guò)組件slot插槽接收父組件傳遞過(guò)來(lái)的彈窗內(nèi)容
3、組件開(kāi)關(guān)的實(shí)現(xiàn):通過(guò)父組件傳遞進(jìn)來(lái)的props控制組件的顯示與隱藏,子組件關(guān)閉時(shí)通過(guò)事件 $emit 觸發(fā)父組件改變狀態(tài)值。
組件代碼實(shí)現(xiàn)
<template>
?? ?<div class="modal-bg" v-show="show">
?? ??? ?<div class="modal-container">
?? ??? ??? ?<div class="modal-header">
?? ??? ??? ??? ?{{title}}
?? ??? ??? ?</div>
?? ??? ??? ?<div class="modal-main">
?? ??? ??? ??? ?<!--?
?? ??? ??? ??? ??? ?如果有多個(gè)插槽時(shí),可以采用具名插槽的方式實(shí)現(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: {
?? ??? ?// 通過(guò)事件綁定及$emit來(lái)執(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 組件庫(kù),如Element-UI等,很少會(huì)直接編寫(xiě)UI樣式代碼之類的操作,可以直接使用第三方庫(kù)完成項(xiàng)目需求。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue二級(jí)菜單導(dǎo)航點(diǎn)擊選中事件的方法
今天小編就為大家分享一篇vue二級(jí)菜單導(dǎo)航點(diǎn)擊選中事件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue通過(guò)krpano.js實(shí)現(xiàn)360全景圖的實(shí)例代碼
這篇文章主要介紹了vue上通過(guò)krpano.js實(shí)現(xiàn)360全景圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10
Vue的transition-group與Virtual Dom Diff算法的使用
這篇文章主要介紹了Vue的transition-group與Virtual Dom Diff算法的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
關(guān)于el-table表格組件中插槽scope.row的使用方式
這篇文章主要介紹了關(guān)于el-table表格組件中插槽scope.row的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue中子組件如何間接修改父組件傳遞過(guò)來(lái)的值問(wèn)題
這篇文章主要介紹了vue中子組件如何間接修改父組件傳遞過(guò)來(lái)的值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
vue項(xiàng)目中jsonp跨域獲取qq音樂(lè)首頁(yè)推薦問(wèn)題
這篇文章主要介紹了vue項(xiàng)目中jsonp跨域獲取qq音樂(lè)首頁(yè)推薦問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法
這篇文章主要介紹了vue+relation-graph繪制關(guān)系圖實(shí)用組件操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端
這篇文章主要介紹了Vue-cli 如何將px轉(zhuǎn)化為rem適配移動(dòng)端,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07

