vue常用組件之confirm用法及說(shuō)明
vue組件之confirm
一些自帶的方法,比如alert,confirm等,往往由于瀏覽器不同而展現(xiàn)出不同的樣式,為了統(tǒng)一,我們可以自己實(shí)現(xiàn)簡(jiǎn)單封裝,下面代碼是vue的一個(gè)組件,它簡(jiǎn)單實(shí)現(xiàn)了confirm功能。
代碼如下:
<!-- * * 確認(rèn)對(duì)話框 * * 使用方法: * <dialogConfirm :show-dialog="showDialog" :ok-text="'刪除'" :cancel-text="'取消'" :content="'content'" v-on:confirm="confirmFn" v-on:cancel="cancelFn" :hide-confirm="false"></dialogConfirm> * * show-dialog: 是否顯示對(duì)話框,布爾值 * ok-text: 確認(rèn)按鈕文字,默認(rèn)為‘好' * cancel-text: 取消按鈕文字,默認(rèn)為‘取消' * hideConfirm: 是否隱藏刪除按鈕 * hideCancle 是否隱藏取消按鈕 * content: 對(duì)話框文字 * confirmFn: 確定按鈕回調(diào) * cancelFn: 取消按鈕回調(diào) --> <template> <div class="dialog" v-if="showDialog"> <transition name="dialog-fade"> <div class="dialog-bg" v-if="showDialog" @click="confirmHide"></div> </transition> <transition name="dialog-show"> <div class="dialog-box" v-if="showDialog"> <div class="dialog-main" v-html="content"></div> <div class="dialog-button"> <div class="dialog-confirm-cancel" @click="clickCancel" v-if="!hideCancle">{{cancelText || '取消'}}</div> <div class="dialog-confirm-button" @click="clickConfirm" v-if="!hideConfirm">{{okText || '好'}}</div> </div> </div> </transition> </div> </template>
<script> ? ? export default{ ? ? ? ? data(){ ? ? ? ? ? ? return{} ? ? ? ? }, ? ? ? ? props: ['showDialog','content','okText','cancelText','hideConfirm','hideCancle'], ? ? ? ? methods: { ? ? ? ? ? ? clickCancel() { ? ? ? ? ? ? ? ? this.$emit('cancel'); ? ? ? ? ? ? }, ? ? ? ? ? ? clickConfirm() { ? ? ? ? ? ? ? ? this.$emit('confirm'); ? ? ? ? ? ? }, ? ? ? ? ? ? confirmHide(){ ? ? ? ? ? ? ? ? this.$emit('confirmhide') ? ? ? ? ? ? } ? ? ? ? } ? ? } </script>
<style lang="sass" rel="stylesheet/scss"> ? .dialog { ? ? position: fixed; ? ? top: 0; left: 0; ? ? width: 100%; ? ? height: 100%; ? ? z-index: 100; ? ?? ? ? &-bg { ? ? ? ? position: absolute; ? ? ? ? top: 0; left: 0; ? ? ? ? width: 100%; ? ? ? ? height: 100%; ? ? ? ? background-color: rgba(0,0,0,.4); ? ? } ? ?? ?&-box { ? ? ? ? width: 5.6rem; ? ? ? ? position: absolute; ? ? ? ? top: 40%; ? ? ? ? left: 50%; ? ? ? ? -webkit-transform: translate3d(-50%,-50%,0); ? ? ? ? transform: translate3d(-50%,-50%,0); ? ? ? ? background-color: #fff; ? ? ? ? border-radius: .1rem; ? ? ? ? line-height: 1.5; ? ? ? ? text-align: center; ?? ?} ?? ?&-main { ?? ? ? ?padding: .42rem .38rem .4rem; ? ? ? ? text-align: left; ? ? ? ? font-size: .28rem; ? ? ? ? color:#333; ?? ?} ?? ?&-button {? ?? ??? ?overflow: hidden; ?? ??? ?border-top: 1px solid #EEE; ? ? ? ? font-size: .32rem; ? ? ? ? line-height: .88rem; ? ? ? ? display: flex; ?? ?} ? ? ? &-confirm { ? ? ? ? &-cancel { ? ? ? ? ? ? color: #3ba3f2; ? ? ? ? ? ? flex:1; ? ? ? ? ? ? border-right: 1px solid #EEE; ? ? ? ? ? ? margin-right: -1px; ? ? ? ? } ? ? ? ? &-button { ? ? ? ? ? ? flex:1; ? ? ? ? ? ? color: #3ba3f2; ? ? ? ? } ? ? } ? ? ? .dialog-show-enter-active, .dialog-fade-enter-active { ? ? ? ? transition: all .3s ease; ? ? } ? ? .dialog-show-leave-active, .dialog-fade-leave-active { ? ? ? ? transition: all .3s ease; ? ? } ? ? .dialog-show-enter, .dialog-show-leave-active { ? ? ? ? top: -35%; ? ? } ? ? .dialog-fade-enter, .dialog-fade-leave-active { ? ? ? ? opacity: 0; ? ? } } ?? ?? </style>
vue自定義confirm彈窗(全局組件)
全局組件方式
全局組件方式采用在main.js文件中進(jìn)行全局注冊(cè)的方式
首先創(chuàng)建mmtoast.vue組件,自定義彈窗的樣式。
<template> ? <div class="quit_box" v-show="visible"> ? ? <div class="topBox"> ? ? ? <span class="tip">提示</span> ? ? </div> ? ? <div class="quit_title">{{message}}</div> ? ? <button class="cancel_btn" @click="leftClick">取消</button> ? ? <button class="confirm_btn" @click="rightClick">確認(rèn)</button> ? </div> </template>
<script> export default { ? name: "mmtoast", ? data() { ? ? return { ? ? ? visible: false, ? ? ? message:'' ? ? }; ? }, ? methods: { ? ? leftClick() { ? ? }, ? ? rightClick() { ? ? }, ? }, }; </script>
接下來(lái)創(chuàng)建index.js文件編寫原生的JS代碼進(jìn)行動(dòng)態(tài)自定義彈窗的插入
import MmToast from './mmtoast.vue' ? let instance let showToast = false const mmToast = { ? ? install(Vue, options = {}) { ? ? ? ? let opt = MmToast.data() // 獲取組件中的默認(rèn)配置 ? ? ? ? Object.assign(opt, options) // 合并配置,最終返回opt以一個(gè)對(duì)象的形式 ? ? ? ? Vue.prototype.$mmToast = (message) => { ? ? ? ? ? ? return new Promise((resolve, reject) => { ? ? ? ? ? ? ? ? if (message) { ? ? ? ? ? ? ? ? ? ? opt.message = message // 如果有傳message,則使用所傳的message ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? /*? ? ? ? ? ? ? ? ? ? ? 解決如何把toast里面的template放到body里面,這個(gè)時(shí)候就可以用到el這個(gè)屬性, ? ? ? ? ? ? ? ? ? ? 但是如果直接把toast組件里面的el這個(gè)屬性,再安裝插件的時(shí)候賦給body是沒(méi)有用的,這個(gè)時(shí)候toast還沒(méi)有渲染出來(lái),那么如何解決呢 ? ? ? ? ? ? ? ? ? ? 就是install方法里面來(lái)手動(dòng)掛載組件,創(chuàng)建一個(gè)構(gòu)造器,然后new那個(gè)構(gòu)造器, ? ? ? ? ? ? ? ? ? ? 創(chuàng)建出一個(gè)組件對(duì)象,然后把這個(gè)對(duì)象掛載到一個(gè)div那里,然后才把內(nèi)容賦給body,做好把這個(gè)構(gòu)造出來(lái)的對(duì)象付給原型? ? ? ? ? ? ? ? ? */ ? ? ? ? ? ? ? ? let TempToastConstructor = Vue.extend(MmToast) // 創(chuàng)建一個(gè)TempToastConstructor組件 ? ? ? ? ? ? ? ? ? instance = new TempToastConstructor({ ? ? ? ? ? ? ? ? ? ? data: opt ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? instance.vm = instance.$mount() ? ? ? ? ? ? ? ? //沒(méi)有掛載到任何文檔中,模板將被渲染為文檔之外的的元素,并且你必須使用原生DOM API把它插入文檔中。該方法返回實(shí)例自身。 ? ? ? ? ? ? ? ? // console.log(instance.vm === instance) ?// 輸出為true ? ? ? ? ? ? ? ? document.body.appendChild(instance.vm.$el)//el對(duì)應(yīng)的就是組件的dom元素 ? ? ? ? ? ? ? ? instance.vm.visible = showToast = true ? ? ? ? ? ? ? ? ? ? instance.rightClick = function () { ? ? ? ? ? ? ? ? ? ? resolve() ? ? ? ? ? ? ? ? ? ? instance.vm.visible = showToast = false ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? instance.leftClick = function () { ? ? ? ? ? ? ? ? ? ? reject() ? ? ? ? ? ? ? ? ? ? instance.vm.visible = showToast = false ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? }) ? ? ? ? } ? ? } } export default mmToast
在mian.js中進(jìn)行全局組件的注冊(cè)
import mmToast from '../src/components/mmtoast/index.js' Vue.use(mmToast)
接下來(lái)在自己的組件中進(jìn)行引入
? ?this.$mmToast("此操作將永久刪除該文件, 是否繼續(xù)?") ? ? ? ? .then(() => { ? ? ? ? ? this.historyList = []; ? ? ? ? ? localStorage.setItem("placeHistory", null); ? ? ? ? ? console.log("刪除成功啦!"); ? ? ? ? }) ? ? ? ? .catch(() => { ? ? ? ? ? console.log("取消刪除啦"); ? ? ? ? });
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue全局注冊(cè)中的kebab-case和PascalCase用法
這篇文章主要介紹了Vue全局注冊(cè)中的kebab-case和PascalCase用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue3.0 CLI - 2.1 - component 組件入門教程
這篇文章主要介紹了vue3.0 CLI - 2.1 - component 組件入門教程,本文主要的關(guān)注點(diǎn)就是組件,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09詳解vue-cli開發(fā)環(huán)境跨域問(wèn)題解決方案
本篇文章主要介紹了vue-cli開發(fā)環(huán)境跨域問(wèn)題解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-06-06vue-i18n的9以上版本中@被用作特殊字符處理,直接用會(huì)報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了vue-i18n的9以上版本中@被用作特殊字符處理,直接用會(huì)報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue3+TS項(xiàng)目中eslint、prettier安裝配置詳細(xì)指南
為了更好的統(tǒng)一項(xiàng)目的代碼風(fēng)格,因此在編寫時(shí)就可以使用eslint+prettier,它們不僅能方便代碼編寫,還能避免不必要的錯(cuò)誤,讓代碼變得更加嚴(yán)謹(jǐn),這篇文章主要給大家介紹了關(guān)于Vue3+TS項(xiàng)目中eslint、prettier安裝配置的相關(guān)資料,需要的朋友可以參考下2024-07-07Vue商品控件與購(gòu)物車聯(lián)動(dòng)效果的實(shí)例代碼
這篇文章主要介紹了Vue商品控件與購(gòu)物車聯(lián)動(dòng)效果的實(shí)例代碼,代碼簡(jiǎn)單易懂非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07