使用 Vue Demi 構(gòu)建通用的 Vue 組件庫
在本文中,我們通過考慮其功能、工作原理以及如何開始使用它來了解 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 中,isvue2 和 isvue3 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,例如 defineComponent、PropType 和h。
現(xiàn)在我們已經(jīng)導(dǎo)入了Vue Demi,讓我們來添加我們的props。這些是用戶在使用組件庫時(shí)需要(或不需要,取決于你的口味)傳入的屬性。
上面看到的屬性是使用 Paystack 的 Popup JS 所必需的。
Popup JS 提供了一種將 Paystack 集成到我們的網(wǎng)站并開始接收付款的簡單方法:
<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<string>,
required: true,
},
email: {
type: String as PropType<string>,
required: true,
},
firstname: {
type: String as PropType<string>,
required: true,
},
lastname: {
type: String as PropType<string>,
required: true,
},
amount: {
type: Number as PropType<number>,
required: true,
},
reference: {
type: String as PropType<string>,
required: true,
},
channels: {
type: Array as PropType<string[]>,
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<MetaData>,
default: () => {},
},
currency: {
type: String as PropType<string>,
default: "",
},
plan: {
type: String as PropType<string>,
default: "",
},
quantity: {
type: String as PropType<string>,
default: "",
},
subaccount: {
type: String as PropType<string>,
default: "",
},
splitCode: {
type: String as PropType<string>,
default: "",
},
transactionCharge: {
type: Number as PropType<number>,
default: 0,
},
bearer: {
type: String as PropType<string>,
default: "",
},
}
</script>
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)建沒有 標(biāo)簽的組件,并返回一個(gè)虛擬 DOM 節(jié)點(diǎn)。
如果你注意到,我們?cè)跅l件語句中使用了Vue Demi的一個(gè)API,isVue2,來有條件地渲染我們的按鈕。如果沒有這一點(diǎn),如果我們想在Vue 2應(yīng)用程序中使用我們的組件庫,我們可能會(huì)因?yàn)閂ue 2不支持Vue 3的一些API而遇到錯(cuò)誤。
現(xiàn)在,當(dāng)我們構(gòu)建我們的庫時(shí),它可以在 Vue 2 和 Vue 3 中訪問。
完整的源代碼可在此處獲得:https://github.com/ECJ222/vue-paystack2
原文:https://blog.logrocket.com/build-universal-vue-component-library-vue-demi/
作者:Enoch Chejieh
本文轉(zhuǎn)載自微信公眾號(hào)「前端全棧開發(fā)者」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系前端全棧開發(fā)者公眾號(hào)。