vue3.0實(shí)現(xiàn)插件封裝
最近公司有一個(gè)新的項(xiàng)目,項(xiàng)目框架是我來負(fù)責(zé)搭建的,所以果斷選擇了Vue3.x+ts。vue3.x不同于vue2.x,他們兩的插件封裝方式完全不一樣。由于項(xiàng)目中需要用到自定義提示框,所以想著自己封裝一個(gè)。vue2.x提供了一個(gè)vue.extend的全局方法。那么vue3.x是不是也會提供什么方法呢?果然從vue3.x源碼中還是找到了。插件封裝的方法,還是分為兩步。
1、組件準(zhǔn)備
按照vue3.x的組件風(fēng)格封裝一個(gè)自定義提示框組件。在props屬性中定義好。需要傳入的數(shù)據(jù)流。
<template> <div v-show="visible" class="model-container"> <div class="custom-confirm"> <div class="custom-confirm-header">{{ title }}</div> <div class="custom-confirm-body" v-html="content"></div> <div class="custom-confirm-footer"> <Button @click="handleOk">{{ okText }}</Button> <Button @click="handleCancel">{{ cancelText }}</Button> </div> </div> </div> </template>
<script lang="ts"> import { defineComponent, watch, reactive, onMounted, onUnmounted, toRefs } from "vue"; export default defineComponent({ name: "ElMessage", props: { title: { type: String, default: "", }, content: { type: String, default: "", }, okText: { type: String, default: "確定", }, cancelText: { type: String, default: "取消", }, ok: { type: Function, }, cancel: { type: Function, }, }, setup(props, context) { const state = reactive({ visible: false, }); function handleCancel() { state.visible = false; props.cancel && props.cancel(); } function handleOk() { state.visible = false; props.ok && props.ok(); } return { ...toRefs(state), handleOk, handleCancel, }; }, }); </script>
2、插件注冊
這個(gè)才是插件封裝的重點(diǎn)。不過代碼量非常少,只有那么核心的幾行。主要是調(diào)用了vue3.x中的createVNode創(chuàng)建虛擬節(jié)點(diǎn),然后調(diào)用render方法將虛擬節(jié)點(diǎn)渲染成真實(shí)節(jié)點(diǎn)。并掛在到真實(shí)節(jié)點(diǎn)上。本質(zhì)上就是vue3.x源碼中的mount操作。
import { createVNode, render } from 'vue'; import type {App} from "vue"; import MessageConstructor from './index.vue' const body=document.body; const Message: any= function(options:any){ const modelDom=body.querySelector(`.container_message`) if(modelDom){ body.removeChild(modelDom) } options.visible=true; const container = document.createElement('div') container.className = `container_message` //創(chuàng)建虛擬節(jié)點(diǎn) const vm = createVNode( MessageConstructor, options, ) //渲染虛擬節(jié)點(diǎn) render(vm, container) document.body.appendChild(container); } export default { //組件祖冊 install(app: App): void { app.config.globalProperties.$message = Message } }
插件封裝完整地址。源碼位置————packages/runtime-core/src/apiCreateApp
中的createAppAPI
函數(shù)中的mount
方法。
以上就是vue3.0實(shí)現(xiàn)插件封裝的詳細(xì)內(nèi)容,更多關(guān)于vue 插件封裝的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題
這篇文章主要介紹了vue?button的@click方法無效鉤子函數(shù)沒有執(zhí)行問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03解決vue-quill-editor上傳內(nèi)容由于圖片是base64的導(dǎo)致字符太長的問題
vue-quill-editor默認(rèn)插入圖片是直接將圖片轉(zhuǎn)為base64再放入內(nèi)容中,如果圖片較多,篇幅太長,就會比較煩惱,接下來通過本文給大家介紹vue-quill-editor上傳內(nèi)容由于圖片是base64的導(dǎo)致字符太長的問題及解決方法,需要的朋友可以參考下2018-08-08Vue.js表單標(biāo)簽中的單選按鈕、復(fù)選按鈕和下拉列表的取值問題
這篇文章主要介紹了Vue.js表單標(biāo)簽中的單選按鈕、復(fù)選按鈕和下拉列表的取值問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11Vue中如何進(jìn)行數(shù)據(jù)響應(yīng)式更新
Vue是一款流行的JavaScript框架,它提供了數(shù)據(jù)響應(yīng)式更新的能力,可以讓我們輕松地更新數(shù)據(jù),并自動更新視圖,本文將介紹Vue中如何進(jìn)行數(shù)據(jù)響應(yīng)式更新,包括使用Vue的響應(yīng)式系統(tǒng)、使用計(jì)算屬性和使用Vue的watcher,需要的朋友可以參考下2023-06-06Vue利用canvas實(shí)現(xiàn)移動端手寫板的方法
本篇文章主要介紹了Vue利用canvas實(shí)現(xiàn)移動端手寫板的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Vue 實(shí)現(xiàn)對quill-editor組件中的工具欄添加title
這篇文章主要介紹了Vue 實(shí)現(xiàn)對quill-editor組件中的工具欄添加title,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08