溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么封裝一個vue中也可使用的uniapp全局彈窗組件

發(fā)布時間:2023-02-23 11:12:18 來源:億速云 閱讀:141 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹了怎么封裝一個vue中也可使用的uniapp全局彈窗組件的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么封裝一個vue中也可使用的uniapp全局彈窗組件文章都會有所收獲,下面我們一起來看看吧。

場景:

當你對接websocket時,或者輪詢也好,你需要獲取到最新的信息,并且在任何頁面彈出一個組件進行后續(xù)操作

思路:

1、先封裝好要彈出的公共組件

2、向vue原型上掛載全局方法,拿到組件真實dom,從而對組件進行顯隱操作

第一步:

創(chuàng)建一個公共組件,以下是組件全部的結構及樣式,你需要把html中的兩個image標簽的路徑換掉或者直接注釋掉也行,html 和 css就不做解釋了

invite.vue 

<template>
  <div class="invite-box">
    <view class="center-box">
      <image class="logo" src="/static/invite-logo.png"></image>
      <image class="close" src="/static/close.png" v-on:click="$closeInvite"></image>
      <view class="title">邀請函</view>
      <view class="content">您好!您的朋友xxx邀請您對<text>“為什么小朋友到了一定年齡需要打疫苗?”</text>進行專家答疑,您是否接受?</view>
      <view class="btn-group">
        <view class="invite-specia">邀請專家</view>
      </view>
    </view>     
  </div>
</template>
 
<script>
 
export default {
  name: 'invite',
 
  props: {
    _specia: String
  },
 
  data() {
    return {}
  },
 
  mounted() {
    console.log('this.specia', this._specia);
  }
}
</script>
 
<style scoped lang='scss'>
.invite-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0,0,0,0.5);
  z-index: 9999;
 
  .center-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 622rpx;
    padding: 32rpx;
    border-radius: 16rpx;
    opacity: 1;
    background: rgba(255,255,255,1);
 
    .logo {
      position: absolute;
      top: -48rpx;
      left: 50%;
      transform: translateX(-50%);
      width: 136rpx;
      height: 144rpx;
    }
    .close {
      position: absolute;
      top: 24rpx;
      right: 24rpx;
      width: 48rpx;
      height: 48rpx;
    }
    
    .title {
      margin-top: 66rpx;
      text-align: center;
      color: rgba(0,0,0,1);
      font-size: 36rpx;
      font-weight: 500;
      font-family: "PingFang SC";
      letter-spacing: 0.6rpx;
    }
 
    .content {
      margin: 40rpx 0;
      font-size: 26rpx;
      font-family: "PingFang SC";
      letter-spacing: 0.6rpx;
      color: #343434;
      text {
        font-size: 32rpx;
        letter-spacing: 0.6rpx;
        color: rgba(69,108,255,1);
      }
    }
 
    .btn-group {
      display: flex;
      justify-content: center;
      padding: 0 16rpx;
      .invite-specia {
        width: 526rpx;
        height: 88rpx;
        line-height: 88rpx;
        border-radius: 16rpx;
        text-align: center;
        background: linear-gradient(-46.8deg, rgba(63,101,255,1) 0%, rgba(97,141,255,1) 100%);
        color: #fff;
      }
    }
  }
}
</style>

第二步:

注冊一個全局函數,使用 Vue.prototype,首先創(chuàng)建一個js文件來存放你的全局方法,之后在main.js中引入掛載

以下代碼中幾個關鍵點:

1、install,參數可以拿到Vue函數,等價于 main.js 中 import 進來的 Vue

2、Vue.extend(Invite),這里可以看到 Invite 是我導入的組件實例對象,該方法傳入組件實例對象可以返回給你該組件的實例構造器,方便我們后續(xù)多次構建并操作該組件

3、instance._props._specia = params,這里只是向新構建的組件內傳遞一個props參數

4、instance.vm = instance.$mount(),掛載模板,生成真實dom,作用和$el一致

5、invite-box是組件最外層盒子的類名

6、setTimeout,因為要添加到最后,需要異步添加

 invite.js

import Invite from '../components/invite.vue'
 
export default {
  install(Vue) {
    const Profile = Vue.extend(Invite)
    
    // 彈出邀請
    Vue.prototype.$openInvite = function(params) {
      const instance = new Profile()
      instance._props._specia = params
      instance.vm = instance.$mount()
      const InviteEle = document.body.lastElementChild
      if(InviteEle.className === 'invite-box') return
      setTimeout(() => document.body.appendChild(instance.vm.$el))
      return instance
    }
 
    // 關閉邀請
    Vue.prototype.$closeInvite = function() {
      const instance = new Profile()
      instance.vm = instance.$mount()
      const InviteEle = document.body.lastElementChild
      if(InviteEle.className !== 'invite-box') return
      document.body.removeChild(InviteEle)
      return instance
    }
  }
}

main.js

// 導入invite.js
import invite from './utils/invite'
// 安裝插件
Vue.use(invite)

第三部:使用

在你任何組件內調用 this.$openInvite() 即可彈出組件,調用 this.$closeInvite()即可關閉組件

關于“怎么封裝一個vue中也可使用的uniapp全局彈窗組件”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“怎么封裝一個vue中也可使用的uniapp全局彈窗組件”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI