vue3自定義插件的作用場(chǎng)景及使用示例詳解
插件的作用場(chǎng)景
在vue2的插件那篇文章我們介紹過插件其實(shí)就是vue的增強(qiáng)功能。通常來為vue添加全局功能的。在vue3中插件的功能也是一樣的,只是它們?cè)诙x上有所不同。
- 通過app.component()和app.directive()注冊(cè)一到多個(gè)全局組件或自定義指令
- 通過app.provide()使一個(gè)資源可被注入進(jìn)整個(gè)應(yīng)用
- 向app.config.globalProperties中添加一些全局實(shí)例屬性或方法
- 一個(gè)可能上述三種都包含了的功能庫(如vue-router)
插件的定義(注冊(cè))
一個(gè)插件可以是一個(gè)擁有 install() 方法的對(duì)象,也可以直接是一個(gè)安裝函數(shù)本身。安裝函數(shù)會(huì)接收到安裝它的應(yīng)用實(shí)例和傳遞給 app.use() 的額外選項(xiàng)作為參數(shù):
下面是我定義的一個(gè)插件,為了方便管理,在src目錄下新建一個(gè)plugins文件夾,根據(jù)插件的功能,文件夾里面可以放置很多js文件。
export default {
install: (app, options) => {
// 注入一個(gè)全局可用的方法
app.config.globalProperties.$myMethod = () => {
console.log('這是插件的全局方法');
}
// 添加全局指令
app.directive('my-directive', {
bind (el, binding, vnode, oldVnode) {
console.log('這是插件的全局指令');
}
})
}
}
插件的安裝
我們一般是安裝在全局的,這樣方便多個(gè)組件使用。
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import myPlugin from './plugins/myPlugin'
createApp(App).use(ElementPlus).use(myPlugin).mount('#app');
插件的使用
在組件中使用
<template>
<div v-my-directive></div>
<el-button @click="clicFunc">測(cè)試按鈕</el-button>
</template>
<script setup>
import { getCurrentInstance } from 'vue';
const ctx = getCurrentInstance();
console.log('ctx', ctx);
const clicFunc = ctx.appContext.app.config.globalProperties.$myMethod();
</script>
效果如下:

插件中的Provide/inject
在插件中,還可以通過provide為插件用戶提供一些內(nèi)容,比如像下面這樣,將options參數(shù)再傳給插件用戶,也就是組件中。
// myPlugin.js
export default {
install: (app, options) => {
// 注入一個(gè)全局可用的方法
app.config.globalProperties.$myMethod = () => {
console.log('這是插件的全局方法');
}
// 添加全局指令
app.directive('my-directive', {
bind () {
console.log('這是插件的全局指令');
}
})
// 將options傳給插件用戶
app.provide('options', options);
}
}
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import myPlugin from './plugins/myPlugin'
createApp(App).use(ElementPlus).use(myPlugin, {
hello: '你好呀'
}).mount('#app');
// 組件中使用
<template>
<div v-my-directive></div>
<el-button @click="clicFunc">測(cè)試按鈕</el-button>
</template>
<script setup>
import { getCurrentInstance, inject } from 'vue';
const ctx = getCurrentInstance();
const hello = inject('options');
console.log('hello', hello);
const clicFunc = ctx.appContext.app.config.globalProperties.$myMethod();
</script>
效果如下:

以上就是vue3自定義插件的作用場(chǎng)景及使用示例詳解的詳細(xì)內(nèi)容,更多關(guān)于vue3自定義插件作用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Electron打包vue文件變成exe應(yīng)用程序的全過程
這篇文章主要給大家介紹了使用Electron打包vue文件變成exe應(yīng)用程序的全過程,文中通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-01-01
vue 使用eventBus實(shí)現(xiàn)同級(jí)組件的通訊
這篇文章主要介紹了vue 使用eventBus實(shí)現(xiàn)同級(jí)組件的通訊,需要的朋友可以參考下2018-03-03
使用vue3+ts+setup獲取全局變量getCurrentInstance的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于使用vue3+ts+setup獲取全局變量getCurrentInstance的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-08-08
el-date-picker默認(rèn)結(jié)束為當(dāng)前時(shí)分秒的操作方法
在element?ui中的日期時(shí)間選擇組件中默認(rèn)是00:00,現(xiàn)在需求是點(diǎn)擊默認(rèn)結(jié)束時(shí)間為當(dāng)前時(shí)分秒,查了很多資料寫的都不準(zhǔn)確?,今天給大家分享el-date-picker默認(rèn)結(jié)束為當(dāng)前時(shí)分秒的操作方法,感興趣的朋友一起看看吧2024-01-01
Vue引入Stylus知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給各位整理的是一篇關(guān)于Vue引入Stylus知識(shí)點(diǎn)總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2020-01-01
Vue表格首列相同數(shù)據(jù)合并實(shí)現(xiàn)方法
這篇文章主要介紹了Vue實(shí)現(xiàn)表格首列相同數(shù)據(jù)合并的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

