Vue使用自定義指令打開(kāi)dialog的實(shí)現(xiàn)方法
完整代碼見(jiàn):https://codesandbox.io/
1. 寫(xiě)一個(gè)dialog
既然要展示一個(gè)dialog,那么首先我們需要準(zhǔn)備一個(gè)dialog,供展示用,如下:實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的dialog,接收message和visible作為參數(shù)
<template> <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" @close="handleClose" > <span>這是一段信息: {{ message }}</span> </el-dialog> </template> <script> export default { components: {}, props: { message: { type: String, default: "", }, visible: { type: Boolean, default: false, }, }, data() { return { dialogVisible: false, }; }, watch: { visible: { handler: function (v) { this.dialogVisible = v; }, immediate: true, }, }, methods: { handleClose() { this.$emit("close"); }, }, }; </script> <style></style>
2. 用自定義指令來(lái)控制dialog
彈窗有了,接下來(lái)實(shí)現(xiàn)個(gè)自定義指令來(lái)打開(kāi)它
import Popup from "../components/Popup.vue"; import Vue from "vue"; /** * 打開(kāi)彈窗 * @param {Object} binding */ const openDialog = (binding) => { const popupComponent = Vue.extend(Popup); const instance = new popupComponent({ propsData: { visible: true, message: binding.value, }, }); instance.$mount(); document.body.appendChild(instance.$el); instance.$on("close", () => { document.body.removeChild(instance.$el); instance.$destroy(); }); }; export default { bind(el, binding) { const handler = () => { openDialog(binding); }; el.addEventListener("click", handler); el._clickHandler = handler; }, unbind: function (el) { el.removeEventListener("click", el._clickHandler); }, };
3. 每次渲染新的dialog
如上述代碼,我們每次打開(kāi)都會(huì)是一個(gè)新的彈窗,所以visible從外面?zhèn)魅肫鋵?shí)沒(méi)什么存在的意義,所以我們將彈窗中的內(nèi)容再改造下,將visible相關(guān)的邏輯刪除,dialogVisible默認(rèn)為true即可。
4.使用該自定義指令
<template> <div id="app"> <div v-popup="msg">點(diǎn)擊打開(kāi)彈窗</div> </div> </template> <script> import popup from "./directives/popup"; export default { name: "App", directives: { popup, }, data() { return { msg: "傳遞的消息", }; }, }; </script>
效果圖示:
到此這篇關(guān)于Vue使用自定義指令打開(kāi)dialog的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Vue打開(kāi)dialog內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue自定義Hook實(shí)現(xiàn)簡(jiǎn)化本地存儲(chǔ)
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)使用 Vue 3 的 Composition API 創(chuàng)建一個(gè)強(qiáng)大而靈活的自定義 Hook,簡(jiǎn)化了在 localStorage 或 sessionStorage 中管理數(shù)據(jù)的流程,需要的可以參考下2023-12-12VUE使用router.push實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和傳參方式
這篇文章主要介紹了VUE使用router.push實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和傳參方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01element-ui中樣式覆蓋問(wèn)題的方法總結(jié)
我們?cè)谑褂胑lement-ui的時(shí)候經(jīng)常會(huì)遇到需要修改組件默認(rèn)樣式,下面這篇文章主要給大家介紹了關(guān)于element-ui中樣式覆蓋問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03vue項(xiàng)目如何從session中獲取對(duì)象,并且使用里面的屬性
這篇文章主要介紹了vue項(xiàng)目如何從session中獲取對(duì)象,并且使用里面的屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12vue中實(shí)現(xiàn)監(jiān)聽(tīng)數(shù)組內(nèi)部元素
這篇文章主要介紹了vue中實(shí)現(xiàn)監(jiān)聽(tīng)數(shù)組內(nèi)部元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vite結(jié)合Vue刪除指定環(huán)境的console.log問(wèn)題
這篇文章主要介紹了Vite結(jié)合Vue刪除指定環(huán)境的console.log問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03