<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          使用 Vue Demi 構(gòu)建通用的 Vue 組件庫

          共 6383字,需瀏覽 13分鐘

           ·

          2022-02-10 01:45

          在本文中,我們通過考慮其功能、工作原理以及如何開始使用它來了解 Vue Demi。

          Vue Demi 是一個(gè)很棒的包,具有很多潛力和實(shí)用性。我強(qiáng)烈建議在創(chuàng)建下一個(gè) Vue 庫時(shí)使用它。

          根據(jù)創(chuàng)建者 Anthony Fu 的說法,Vue Demi 是一個(gè)開發(fā)實(shí)用程序,它允許用戶為 Vue 2 和 Vue 3 編寫通用的 Vue 庫,而無需擔(dān)心用戶安裝的版本。

          以前,要?jiǎng)?chuàng)建支持兩個(gè)目標(biāo)版本的 Vue 庫,我們會(huì)使用不同的分支來分離對(duì)每個(gè)版本的支持。對(duì)于現(xiàn)有庫來說,這是一個(gè)很好的方法,因?yàn)樗鼈兊拇a庫通常更穩(wěn)定。

          缺點(diǎn)是,你需要維護(hù)兩個(gè)代碼庫,這讓你的工作量翻倍。對(duì)于想要支持Vue的兩個(gè)目標(biāo)版本的新Vue庫來說,我不推薦這種方法。實(shí)施兩次功能請(qǐng)求和錯(cuò)誤修復(fù)根本就不理想。

          這就是 Vue Demi 的用武之地。Vue Demi 通過為兩個(gè)目標(biāo)版本提供通用支持來解決這個(gè)問題,這意味著您只需構(gòu)建一次即可獲得兩個(gè)目標(biāo)版本的所有優(yōu)點(diǎn),從而獲得兩全其美的優(yōu)勢(shì)。

          在本文中,我們將了解 Vue Demi 是做什么的,它是如何工作的,以及如何開始構(gòu)建一個(gè)通用的 Vue 組件庫。

          Vue Demi 中的額外 API

          除了 Vue 已經(jīng)提供的 API 之外,Vue Demi 還引入了一些額外的 API 來幫助區(qū)分用戶的環(huán)境和執(zhí)行特定于版本的邏輯。讓我們仔細(xì)看看它們!

          請(qǐng)注意,Vue Demi 還包括 Vue 中已經(jīng)存在的標(biāo)準(zhǔn) API,例如 ref、onMounted 和 onUnmounted 等。

          isVue2 and isVue3

          在 Vue Demi 中,isvue2isvue3 API 允許用戶在創(chuàng)建 Vue 庫時(shí)應(yīng)用特定于版本的邏輯。

          例如:

          import { isVue2, isVue3 } from 'vue-demi' 
          if (isVue2) {
          // Vue 2 only
          } else {
          // Vue 3 only
          }

          vue2

          Vue Demi 提供了 vue2 API,它允許用戶訪問 Vue 2 的全局 API,如下所示:

          import { Vue2 } from 'vue-demi' 
          // in Vue 3 `Vue2` will return undefined
          if (Vue2) {
          Vue2.config.devtools = true
          }

          install()

          在 Vue 2 中,Composition API 作為插件提供,在使用它之前需要安裝在 Vue 實(shí)例上:

          import Vue from 'vue' 
          import VueCompositionAPI from '@vue/composition-api'

          Vue.use(VueCompositionAPI)

          Vue Demi 會(huì)嘗試自動(dòng)安裝它,但是對(duì)于您想要確保插件安裝正確的情況,提供了 install() API 來幫助您。

          它作為 Vue.use(VueCompositionAPI) 的安全版本公開:

          import { install } from 'vue-demi' 

          install()

          Vue Demi 入門

          要開始使用 Vue Demi,您需要將其安裝到您的應(yīng)用程序中。在本文中,我們將創(chuàng)建一個(gè)集成 Paystack 支付網(wǎng)關(guān)的 Vue 組件庫。

          你可以像這樣安裝 Vue Demi:

          // Npm 
          npm i vue-demi

          // Yarn
          yarn add vue-demi

          您還需要添加 vue@vue/composition-api 作為庫的對(duì)等依賴項(xiàng),以指定它應(yīng)該支持的版本。

          現(xiàn)在我們可以將 Vue Demi 導(dǎo)入我們的應(yīng)用程序:

          <script lang="ts"> 
          import {defineComponent, PropType, h, isVue2} from "vue-demi"

          export default defineComponent({
          // ...
          })
          script
          >

          如此處所示,我們可以使用已經(jīng)存在的標(biāo)準(zhǔn) Vue API,例如 defineComponentPropTypeh

          現(xiàn)在我們已經(jīng)導(dǎo)入了Vue Demi,讓我們來添加我們的props。這些是用戶在使用組件庫時(shí)需要(或不需要,取決于你的口味)傳入的屬性。

          <script lang="ts">
          import {defineComponent, PropType, h, isVue2} from "vue-demi"
          // Basically this tells the metadata prop what kind of data is should accept
          interface MetaData {
          [key: string]: any
          }

          export default defineComponent({
          props: {
          paystackKey: {
          type: String as PropType,
          required: true,
          },
          email: {
          type: String as PropType,
          required: true,
          },
          firstname: {
          type: String as PropType,
          required: true,
          },
          lastname: {
          type: String as PropType,
          required: true,
          },
          amount: {
          type: Number as PropType,
          required: true,
          },
          reference: {
          type: String as PropType,
          required: true,
          },
          channels: {
          type: Array as PropType,
          default: () => ["card", "bank"],
          },
          callback: {
          type: Function as PropType<(response: any) => void>,
          required: true,
          },
          close: {
          type: Function as PropType<() => void>,
          required: true,
          },
          metadata: {
          type: Object as PropType,
          default: () => {},
          },
          currency: {
          type: String as PropType,
          default: "",
          },
          plan: {
          type: String as PropType,
          default: "",
          },
          quantity: {
          type: String as PropType,
          default: "",
          },
          subaccount: {
          type: String as PropType,
          default: "",
          },
          splitCode: {
          type: String as PropType,
          default: "",
          },
          transactionCharge: {
          type: Number as PropType,
          default: 0,
          },
          bearer: {
          type: String as PropType,
          default: "",
          },
          }
          script
          >

          上面看到的屬性是使用 Paystack 的 Popup JS 所必需的。

          Popup JS 提供了一種將 Paystack 集成到我們的網(wǎng)站并開始接收付款的簡單方法:

          data() {
          return {
          scriptLoaded: false,
          }
          },
          created() {
          this.loadScript()
          },
          methods: {
          async loadScript(): Promise<void> {
          const scriptPromise = new Promise((resolve) => {
          const script: any = document.createElement("script")
          script.defer = true
          script.src = "https://js.paystack.co/v1/inline.js"
          // Add script to document head
          document.getElementsByTagName("head")[0].appendChild(script)
          if (script.readyState) {
          // IE support
          script.onreadystatechange = () => {
          if (script.readyState === "complete") {
          script.onreadystatechange = null
          resolve(true)
          }
          }
          } else {
          // Others
          script.onload = () => {
          resolve(true)
          }
          }
          })
          this.scriptLoaded = await scriptPromise
          },
          payWithPaystack(): void {
          if (this.scriptLoaded) {
          const paystackOptions = {
          key: this.paystackKey,
          email: this.email,
          firstname: this.firstname,
          lastname: this.lastname,
          channels: this.channels,
          amount: this.amount,
          ref: this.reference,
          callback: (response: any) => {
          this.callback(response)
          },
          onClose: () => {
          this.close()
          },
          metadata: this.metadata,
          currency: this.currency,
          plan: this.plan,
          quantity: this.quantity,
          subaccount: this.subaccount,
          split_code: this.splitCode,
          transaction_charge: this.transactionCharge,
          bearer: this.bearer,
          }
          const windowEl: any = window
          const handler = windowEl.PaystackPop.setup(paystackOptions)
          handler.openIframe()
          }
          },
          },

          scriptLoaded 狀態(tài)幫助我們知道是否添加了 Paystack Popup JS 腳本,并且 loadScript 方法加載 Paystack Popup JS 腳本并將其添加到我們的文檔頭部。

          payWithPaystack 方法用于在調(diào)用時(shí)使用 Paystack Popup JS 初始化交易:

          render() {
          if (isVue2) {
          return h(
          "button",
          {
          staticClass: ["paystack-button"],
          style: [{display: "block"}],
          attrs: {type: "button"},
          on: {click: this.payWithPaystack},
          },
          this.$slots.default ? this.$slots.default : "PROCEED TO PAYMENT"
          )
          }
          return h(
          "button",
          {
          class: ["paystack-button"],
          style: [{display: "block"}],
          type: "button",
          onClick: this.payWithPaystack,
          },
          this.$slots.default ? this.$slots.default() : "PROCEED TO PAYMENT"
          )
          }

          render 函數(shù)幫助我們創(chuàng)建沒有