Vue關(guān)閉當(dāng)前彈窗頁(yè)面的兩種方式
在 Vue 中關(guān)閉當(dāng)前彈窗頁(yè)面可以通過以下兩種方式實(shí)現(xiàn):
1、使用關(guān)閉按鈕觸發(fā)關(guān)閉事件
在彈窗組件中添加一個(gè)關(guān)閉按鈕,并在點(diǎn)擊事件中調(diào)用關(guān)閉彈窗的方法。例如:
<template>
<div class="modal">
<div class="modal-content">
<span class="close" @click="closeModal">×</span>
<p>彈窗內(nèi)容</p>
</div>
</div>
</template>
<script>
export default {
methods: {
closeModal() {
this.$emit('close');
}
}
}
</script>在父組件中監(jiān)聽關(guān)閉事件,并將彈窗的顯示狀態(tài)設(shè)置為 false。例如:
<template>
<div>
<button @click="showModal = true">打開彈窗</button>
<modal v-if="showModal" @close="showModal = false"></modal>
</div>
</template>
<script>
import Modal from './Modal.vue';
export default {
components: {
Modal
},
data() {
return {
showModal: false
}
}
}
</script>2、使用路由守衛(wèi)關(guān)閉彈窗
在打開彈窗的路由中添加一個(gè) beforeRouteLeave 路由守衛(wèi),當(dāng)離開該路由時(shí)關(guān)閉彈窗。例如:
const router = new VueRouter({
routes: [
{
path: '/modal',
component: Modal,
beforeRouteLeave(to, from, next) {
this.$emit('close');
next();
}
}
]
});在父組件中監(jiān)聽關(guān)閉事件,并在關(guān)閉事件中調(diào)用關(guān)閉彈窗的方法。例如:
<template>
<div>
<button @click="showModal = true">打開彈窗</button>
<modal v-if="showModal" @close="showModal = false"></modal>
</div>
</template>
<script>
import Modal from './Modal.vue';
export default {
components: {
Modal
},
data() {
return {
showModal: false
}
}
}
</script>以上兩種方式都可以實(shí)現(xiàn)關(guān)閉當(dāng)前彈窗頁(yè)面的功能,具體使用哪種方式取決于項(xiàng)目需求和個(gè)人習(xí)慣。
總結(jié)
到此這篇關(guān)于Vue關(guān)閉當(dāng)前彈窗頁(yè)面的兩種方式的文章就介紹到這了,更多相關(guān)Vue關(guān)閉當(dāng)前彈窗頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Vue 3中使用OpenLayers讀取WKB數(shù)據(jù)并顯示圖形效果
WKB作為一種緊湊的二進(jìn)制格式,在處理和傳輸空間數(shù)據(jù)時(shí)具有明顯優(yōu)勢(shì),本文介紹了如何在Vue 3中使用OpenLayers讀取WKB格式的空間數(shù)據(jù)并顯示圖形,感興趣的朋友一起看看吧2024-12-12
vue3?內(nèi)容過多出現(xiàn)滾動(dòng)條時(shí)滾動(dòng)條自動(dòng)定位到末端的操作代碼
這篇文章主要介紹了vue3?內(nèi)容過多出現(xiàn)滾動(dòng)條時(shí)滾動(dòng)條自動(dòng)定位到末端的操作代碼,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-05-05
vscode中vue-cli項(xiàng)目es-lint的配置方法
本文主要介紹vscode中 vue項(xiàng)目es-lint的配置方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的的朋友參考下吧2018-07-07
Vue3+vueuse實(shí)現(xiàn)放大鏡示例詳解
這篇文章主要為大家介紹了Vue3+vueuse實(shí)現(xiàn)放大鏡示例過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
詳解Vue的數(shù)據(jù)及事件綁定和filter過濾器
這篇文章主要為大家介紹了Vue的數(shù)據(jù)及事件綁定和filter過濾器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
el-element中el-table表格嵌套el-select實(shí)現(xiàn)動(dòng)態(tài)選擇對(duì)應(yīng)值功能
這篇文章主要給大家介紹了關(guān)于el-element中el-table表格嵌套el-select實(shí)現(xiàn)動(dòng)態(tài)選擇對(duì)應(yīng)值功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01
Vue 開發(fā)音樂播放器之歌手頁(yè)右側(cè)快速入口功能
這篇文章主要介紹了Vue 開發(fā)音樂播放器之歌手頁(yè)右側(cè)快速入口功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08

