Vue自定義全局彈窗組件操作
寫在前面
頁面中會(huì)有很多時(shí)候需要彈窗提示,我們可以寫一個(gè)彈窗組件,但是如果每個(gè)頁面都引入這個(gè)組件,太麻煩了,所以我們將它變成全局組件,需要用的時(shí)候直接通過JS調(diào)用即可,不需要在每個(gè)頁面引入了
效果圖
彈窗組件
新建一個(gè)彈窗的組件——popup.vue
<template> <transition name='fade'> <!-- 蒙版 --> <div class="mask" v-if="show" @touchmove.prevent> <div class="window"> <img class="shadow" :src="imgUrl" alt=""> <h1>{{title}}</h1> <p>{{content}}</p> <button @click="btnClick">{{btnText}}</button> </div> <img @click="show = false" v-fb class="close" src="../../../pages/signin/dialog/images/關(guān)閉.png" alt=""> </div> </transition> </template> <script> export default { name: 'app', data () { return { show: false, imgUrl: '', title: '', content: '', btnText: '', click: '' } }, created () { }, methods: { btnClick () { this.click() this.show = false } } } </script> <style lang="less" scoped> @import "../../../assets/css/minx/minx"; @import "../../../assets/css/pixel/pixel"; // 漸變過渡 .fade-enter, .fade-leave-active { opacity: 0; } .fade-enter-active, .fade-leave-active { transition: opacity .35s; } // 全局彈窗 .mask { .fixed; z-index: 10; background:rgba(0,0,0,0.65); display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 75/75rem; .window{ height: 400/75rem; width: 100%; background: #fff; border-radius:8px; text-align: center; .shadow{ width: 270/75rem; margin-top: -90/75rem; } h1{ margin-top: 32/75rem; font-size:32/75rem; color:#f1300b; line-height:32/75rem; font-weight: 600; } p{ margin-top: 32/75rem; font-size:28/75rem; color:#222222; line-height:40/75rem; } button{ margin-top: 34/75rem; background:#f95644; border-radius:10px; width:23/75remx; height:64/75rem; font-size:28/75rem; color:#ffffff; } } .close{ width:60/75rem; height:60/75rem; margin-top: 40/75rem; } } </style>
popup.js文件
新建一個(gè)popup.js文件,寫方法
import Vue from 'vue' import Popup from './popup.vue' const PopupBox = Vue.extend(Popup) Popup.install = function (data) { let instance = new PopupBox({ data }).$mount() document.body.appendChild(instance.$el) Vue.nextTick(() => { instance.show = true // show 和彈窗組件里的show對(duì)應(yīng),用于控制顯隱 }) } export default Popup
main.js引入popup.js
// 自定義全局彈窗組件 import Vue from 'vue' import Popup from './components/dialog/popup' Vue.prototype.$popup = Popup.install
組件中使用方法
methods: { btnClick () { this.$popup({ imgUrl: require('../../../static/images/shadow.png'), // 頂部圖片 title: '我是標(biāo)題', content: '我是內(nèi)容', btnText: '我是按鈕', click: () => { // 點(diǎn)擊按鈕事件 this.$router.push('……') } }) } }
方便以后自己使用,大家也可以參考哦,也希望大家多多支持腳本之家,謝謝~~
相關(guān)文章
vue-cli腳手架創(chuàng)建項(xiàng)目遇到的坑及解決
這篇文章主要介紹了vue-cli腳手架創(chuàng)建項(xiàng)目遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01vue項(xiàng)目代碼格式規(guī)范設(shè)置參考指南
這篇文章主要給大家介紹了關(guān)于vue3簡單封裝input組件和統(tǒng)一表單數(shù)據(jù)的相關(guān)資料,不管你學(xué)習(xí)哪一門編程語言,相信大家都會(huì)略化這一部分,需要的朋友可以參考下2022-05-05vue中使用element ui的input框?qū)崿F(xiàn)模糊搜索的輸入框
這篇文章主要介紹了vue中使用element ui的input框?qū)崿F(xiàn)模糊搜索的輸入框,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11vue使用axios實(shí)現(xiàn)excel文件下載的功能
這篇文章主要介紹了vue中使用axios實(shí)現(xiàn)excel文件下載的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07在Vant的基礎(chǔ)上實(shí)現(xiàn)添加表單驗(yàn)證框架的方法示例
這篇文章主要介紹了在Vant的基礎(chǔ)上實(shí)現(xiàn)添加驗(yàn)證框架的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12vue?結(jié)合webpack的初級(jí)使用指南小白學(xué)習(xí)篇
這篇文章主要為大家介紹了vue?結(jié)合webpack的初級(jí)使用指南非常適合入門webpack的小白學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05vue+element-ui?校驗(yàn)開始時(shí)間與結(jié)束時(shí)間的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+element-ui?校驗(yàn)開始時(shí)間與結(jié)束時(shí)間的代碼實(shí)現(xiàn),最主要的需求是開始時(shí)間不能早于當(dāng)前時(shí)間,感興趣的朋友跟隨小編一起看看吧2024-07-07