最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年11月20日 11:32:36   作者:陳山豆  
這篇文章主要介紹了vue自定義插件封裝,實現(xiàn)簡易的elementUi的Message和MessageBox的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下

  這次封裝基于vuecli3 + typescript實現(xiàn),javascript也是同理,沒有區(qū)別;

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

1、實現(xiàn)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>

然后書寫對應(yīng)的toast.ts

import Vue from 'vue';
// toast組件
import toastComponent from '@/components/toast/index.vue'

// 返回一個 擴展實例構(gòu)造器
const ToastConstructor = Vue.extend(toastComponent)

// 定義彈出組件的函數(shù) 接收2個參數(shù), 要顯示的文本 和 顯示時間
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)
}
// 注冊為全局組件的函數(shù)
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'})

效果如下:

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

  主要用于操作的二次確定

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

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

<template>
  <div class="confirm-wrapper" @click="confirmClick($event)">
    <div class="confirm-box" 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" class="define-button" 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>

對應(yīng)的書寫confirm.ts

這里使用Promise來為用戶點擊確定或者取消做對應(yīng)的交互觸發(fā)

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(() => {});

效果如下

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

typescript對應(yīng)的聲明文件

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

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

以上就是vue自定義插件封裝,實現(xiàn)簡易的elementUi的Message和MessageBox的示例的詳細內(nèi)容,更多關(guān)于vue自定義插件封裝的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue實現(xiàn)的雙向數(shù)據(jù)綁定操作示例

    vue實現(xiàn)的雙向數(shù)據(jù)綁定操作示例

    這篇文章主要介紹了vue實現(xiàn)的雙向數(shù)據(jù)綁定操作,結(jié)合完整實例形式較為詳細的分析了vue.js進行數(shù)據(jù)雙向綁定操作的常見實現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • 使用Vue實現(xiàn)Markdown文檔的展示和解析

    使用Vue實現(xiàn)Markdown文檔的展示和解析

    在Vue項目中,Markdown文檔的使用越來越普遍,因此在Vue中如何進行Markdown文檔展示與解析也成為了一個熱門話題,本文將介紹如何使用Vue實現(xiàn)Markdown文檔的展示和解析,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下
    2024-01-01
  • 深度了解vue.js中hooks的相關(guān)知識

    深度了解vue.js中hooks的相關(guān)知識

    這篇文章主要介紹了深度了解vue.js中hooks的相關(guān)知識,生命周期鉤子提供了一些 方法 ,因此你可以在組件生命周期的不同時刻精確地觸發(fā)某些操作。當(dāng)我們將組件實例化時,組件會被創(chuàng)建,反之會被銷毀。,需要的朋友可以參考下
    2019-06-06
  • 基于vue2的table分頁組件實現(xiàn)方法

    基于vue2的table分頁組件實現(xiàn)方法

    這篇文章主要為大家詳細介紹了基于vue2的table分頁組件實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue+echart實現(xiàn)圓滑折線圖

    vue+echart實現(xiàn)圓滑折線圖

    這篇文章主要為大家詳細介紹了vue+echart實現(xiàn)圓滑折線圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 前端vue3樹形組件使用代碼示例

    前端vue3樹形組件使用代碼示例

    最近在開發(fā)時遇到一個問題,是在輸入框里面放一個樹形組件,這篇文章主要給大家介紹了關(guān)于前端vue3樹形組件使用的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-07-07
  • vue中如何獲取當(dāng)前路由地址

    vue中如何獲取當(dāng)前路由地址

    這篇文章主要介紹了vue中如何獲取當(dāng)前路由地址,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue組件通信傳遞數(shù)據(jù)的三種方式

    Vue組件通信傳遞數(shù)據(jù)的三種方式

    這篇文章主要介紹了Vue組件通信傳遞數(shù)據(jù)的三種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-04-04
  • Vue.js中對css的操作(修改)具體方式詳解

    Vue.js中對css的操作(修改)具體方式詳解

    使用v-bind:class或者v-bind:style或者直接通過操作dom來對其樣式進行更改;接下來通過本文給大家分享Vue.js中對css的操作(修改)具體方式,感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • 詳解如何運行vue項目

    詳解如何運行vue項目

    這篇文章主要介紹了如何運行vue項目,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論

乳山市| 新安县| 福安市| 静安区| 桦川县| 陵川县| 宽甸| 延庆县| 大化| 开平市| 德清县| 湘阴县| 汽车| 大洼县| 柞水县| 辽中县| 凤山县| 铜山县| 微山县| 乌鲁木齐市| 永靖县| 卢龙县| 招远市| 山阴县| 抚州市| 江油市| 雷山县| 双城市| 玉田县| 东海县| 哈密市| 都兰县| 赞皇县| 平谷区| 区。| 宁陵县| 枝江市| 淮滨县| 淄博市| 监利县| 塔河县|