Vue關(guān)閉當(dāng)前彈窗頁面的兩種方式
在 Vue 中關(guān)閉當(dāng)前彈窗頁面可以通過以下兩種方式實現(xiàn):
1、使用關(guān)閉按鈕觸發(fā)關(guān)閉事件
在彈窗組件中添加一個關(guā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)閉彈窗
在打開彈窗的路由中添加一個 beforeRouteLeave 路由守衛(wèi),當(dāng)離開該路由時關(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>
以上兩種方式都可以實現(xiàn)關(guān)閉當(dāng)前彈窗頁面的功能,具體使用哪種方式取決于項目需求和個人習(xí)慣。
總結(jié)
到此這篇關(guān)于Vue關(guān)閉當(dāng)前彈窗頁面的兩種方式的文章就介紹到這了,更多相關(guān)Vue關(guān)閉當(dāng)前彈窗頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Vue 3中使用OpenLayers讀取WKB數(shù)據(jù)并顯示圖形效果
WKB作為一種緊湊的二進制格式,在處理和傳輸空間數(shù)據(jù)時具有明顯優(yōu)勢,本文介紹了如何在Vue 3中使用OpenLayers讀取WKB格式的空間數(shù)據(jù)并顯示圖形,感興趣的朋友一起看看吧2024-12-12vue3?內(nèi)容過多出現(xiàn)滾動條時滾動條自動定位到末端的操作代碼
這篇文章主要介紹了vue3?內(nèi)容過多出現(xiàn)滾動條時滾動條自動定位到末端的操作代碼,本文給大家介紹的非常詳細,需要的朋友參考下吧2024-05-05詳解Vue的數(shù)據(jù)及事件綁定和filter過濾器
這篇文章主要為大家介紹了Vue的數(shù)據(jù)及事件綁定和filter過濾器,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01el-element中el-table表格嵌套el-select實現(xiàn)動態(tài)選擇對應(yīng)值功能
這篇文章主要給大家介紹了關(guān)于el-element中el-table表格嵌套el-select實現(xiàn)動態(tài)選擇對應(yīng)值功能的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-01-01Vue 開發(fā)音樂播放器之歌手頁右側(cè)快速入口功能
這篇文章主要介紹了Vue 開發(fā)音樂播放器之歌手頁右側(cè)快速入口功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08