色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

vue自定義插件封裝,實現簡易的elementUi的Message和MessageBox的示例

瀏覽:95日期:2022-10-24 14:08:06

這次封裝基于vuecli3 + typescript實現,javascript也是同理,沒有區別;

自定義插件有助于我們可以將一些頁面交互封裝并在js或ts文件中引入實現,而不是只在 .vue文件。

1、實現toast插件封裝(類似簡易版的elementUi的message)

先書寫一個toast組件

<template> <div ref='toastRef' class='toastMessageBox'>{{ message }}</div></template><script lang='ts'>import { Component, Vue, Watch } from ’vue-property-decorator’;@Component({})export default class toast extends Vue { message: string = ’’; type: string = ’’; mounted() { let ele: HTMLElement = <HTMLElement>this.$refs.toastRef; if (this.type === ’success’) { ele.style.backgroundColor = ’#f0f9eb’; ele.style.borderColor = ’#e1f3d8’; ele.style.color = ’#67c23a’; } else if (this.type === ’error’) { ele.style.backgroundColor = ’#fef0f0’; ele.style.borderColor = ’#fde2e2’; ele.style.color = ’#f56c6c’; } } showToast() { let ele: HTMLElement = <HTMLElement>this.$refs.toastRef; ele.style.top = ’20px’; ele.style.opacity = ’1’; } hideToast() { let ele: HTMLElement = <HTMLElement>this.$refs.toastRef; ele.style.top = ’-100px’; ele.style.opacity = ’0’; }}</script><style scoped lang='scss'>.toastMessageBox { position: fixed; min-width: 380px; left: 50%; z-index: 999; -webkit-transform: translateX(-50%); transform: translateX(-50%); color: #fff; padding: 15px 15px 15px 20px; font-size: 16px; border-radius: 4px; opacity: 0; top: -100px; transition: opacity 0.3s, top 0.4s; color: #909399; background-color: #edf2fc; border: 1px solid #ebeef5;}</style>

然后書寫對應的toast.ts

import Vue from ’vue’;// toast組件import toastComponent from ’@/components/toast/index.vue’// 返回一個 擴展實例構造器const ToastConstructor = Vue.extend(toastComponent)// 定義彈出組件的函數 接收2個參數, 要顯示的文本 和 顯示時間function showToast(data: { message: any, type: string, duration?: number }) { // 實例化一個 toast.vue const toastDom: any = new ToastConstructor({ el: document.createElement(’div’), data() { return {message: data.message,type: data.type, } } }); // 把 實例化的 toast.vue 添加到 body 里 document.body.appendChild(toastDom.$el); setTimeout(() => { toastDom.showToast(); }) // 過了 duration 時間后隱藏 let duration = data.duration ? data.duration : 2000 setTimeout(() => { toastDom.hideToast(); setTimeout(() => { document.body.removeChild(toastDom.$el) }, 500) }, duration)}// 注冊為全局組件的函數function registryToast() { // 將組件注冊到 vue 的 原型鏈里去, // 這樣就可以在所有 vue 的實例里面使用 this.$toast() Vue.prototype.$toast = showToast}export default registryToast;

然后在main.ts中注冊

// 自定義toast插件import toastMessage from ’@/utils/toast.ts’;Vue.use(toastMessage)

然后就可以在全局地方使用

this.$toast({message:'成功',type:’success’})

效果如下:

vue自定義插件封裝,實現簡易的elementUi的Message和MessageBox的示例

2、實現$confirm插件封裝(類似簡易版的elementUi的messageBox)

主要用于操作的二次確定

還是一樣,首先書寫confirm組件

這里按鈕點擊事件我設置了一個callback回調,用于方便后面的操作交互

<template> <div @click='confirmClick($event)'> <div ref='confirmBox'> <p class='confirm-title'>{{ title }} </p> <p class='content-text'>{{ contentText }} </p> <div class='footer-button'><ck-button size='mini' @click='close'>取消</ck-button><ck-button size='mini' type='primary' @click='define'>確定</ck-button> </div> </div> </div></template><script lang='ts'>import { Component, Vue, Watch } from ’vue-property-decorator’;@Component({})export default class confirm extends Vue { title: string = ’提示’; contentText: string = ’’; callback: any = null; confirmClick(e: any) { let confirmBox = this.$refs.confirmBox; if (e.target.contains(confirmBox)) { (<HTMLElement>this.$el.parentNode).removeChild(this.$el); } } define() { (<HTMLElement>this.$el.parentNode).removeChild(this.$el); this.callback(’confirm’); } close() { (<HTMLElement>this.$el.parentNode).removeChild(this.$el); this.callback(’cancel’); }}</script><style scoped lang='scss'>.confirm-wrapper { position: fixed; top: 0; bottom: 0; right: 0; left: 0; background: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; .confirm-box { width: 420px; padding-bottom: 10px; vertical-align: middle; background-color: #fff; border-radius: 4px; border: 1px solid #ebeef5; font-size: 18px; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); text-align: left; overflow: hidden; backface-visibility: hidden; .confirm-title { padding: 15px 15px 10px; font-size: 18px; } .content-text { padding: 10px 15px; color: #606266; font-size: 14px; } .footer-button { padding-top: 24px; display: flex; justify-content: flex-end; padding-right: 24px; .define-button {margin-left: 16px; } } }}</style>

對應的書寫confirm.ts

這里使用Promise來為用戶點擊確定或者取消做對應的交互觸發

import Vue from ’vue’;import confirm from ’@/components/confirm/index.vue’;const confirmConstructor = Vue.extend(confirm);const showConfirm = (contentText: string) => { return new Promise((reslove, reject) => { const confirmDom: any = new confirmConstructor({ el: document.createElement(’template’), data() {return { contentText,} }, }) confirmDom.callback = (action: string) => { if (action === ’confirm’) {reslove() } else if (action === ’cancel’) {reject() } } document.body.appendChild(confirmDom.$el); })}function registryConfirm() { // 將組件注冊到 vue 的 原型鏈里去, // 這樣就可以在所有 vue 的實例里面使用 this.$toast() Vue.prototype.$confirm = showConfirm}export default registryConfirm;

接下來在main.ts中

import registryConfirm from ’@/utils/confirm.ts’;Vue.use(registryConfirm)

然后就可以在全局使用

this.$confirm(’是否確認刪除’) .then(() => { this.$toast({ message: ’刪除成功’, type: ’success’, }); }) .catch(() => {});

效果如下

vue自定義插件封裝,實現簡易的elementUi的Message和MessageBox的示例

這時,點擊確定按鈕就會觸發 .then里的事件,點擊取消則觸發 .catch里的事件

typescript對應的聲明文件

typescript書寫自定義插件對應的聲明文件,避免編輯報錯

import Vue from 'vue';declare module 'vue/types/vue' { interface Vue { $toast: any, $confirm: any }}

以上就是vue自定義插件封裝,實現簡易的elementUi的Message和MessageBox的示例的詳細內容,更多關于vue自定義插件封裝的資料請關注好吧啦網其它相關文章!

標簽: Vue
主站蜘蛛池模板: 久久99网站 | 成人精品网| 日韩精品久久久毛片一区二区 | 黄色美女网站视频 | 久久精品国产亚洲欧美 | 中国一级特黄剌激爽毛片 | 日本亚洲视频 | 黄色毛片免费在线观看 | 国内精品影院久久久久 | 成人午夜毛片在线看 | 午夜免费片在线观看不卡 | 亚洲人成在线免费观看 | 国产不卡影院 | 大狠狠大臿蕉香蕉大视频 | 国产一在线精品一区在线观看 | 自拍一页 | 福利视频美女国产精品 | 91免费版网站 | 女人成午夜大片7777在线 | 污全彩肉肉无遮挡彩色 | 一级毛片视频免费观看 | 国产日韩欧美自拍 | 一级在线视频 | 久久免费视频99 | 一色屋成人免费精品网站 | 黄色三级网站 | 性福利视频 | 久久人 | 深夜成人性视频免费看 | 国产一区二区fc2ppv在线播放 | 成年人免费黄色片 | 亚洲精品亚洲人成毛片不卡 | 最新精品亚洲成a人在线观看 | 亚洲一区二区影视 | 久久久www免费人成看片 | 亚洲久久久久 | 国产成人精品男人的天堂网站 | 一区二区三区免费精品视频 | 草草影院欧美三级日本 | 国产性大片黄在线观看在线放 | 精品亚洲成a人在线观看 |