Vue3 自定义插件(plugin)

文章目录

  • [Vue3 自定义插件(plugin)](#Vue3 自定义插件(plugin))

Vue3 自定义插件(plugin)

概述

插件 (Plugins) 是一种能为 Vue 添加全局功能的工具代码。

用法

Vue3 插件有2种定义方式:

  • 对象形式。对象中有install方法。
  • 函数形式。函数本身就是安装方法,其中,函数可以接收两个参数,分别是安装它的应用实例和传递给app.use的额外选项。

定义插件:

对象形式:

javascript 复制代码
import Header from "../components/Header.vue";

// 对象方式:
const myPlugin = {
    install(app, options) {
        // 配置全局方法
        app.config.globalProperties.globalMethod = function (value) {
            return value.toLowerCase();
        };
        // 注册公共组件
        app.component("Header", Header);
        // 注册公共指令
        app.directive("upper", function (el, binding) {
            // 转为大写
            el.textContent = binding.value.toUpperCase();
            if (binding.arg === "small") {
                el.style.fontSize = options.small + "px";
            } else if (binding.arg === "medium") {
                el.style.fontSize = options.medium + "px";
            } else {
                el.style.fontSize = options.large + "px";
            }
        });
    }
};

export default myPlugin;

函数形式:

javascript 复制代码
import Header from "../components/Header.vue";

// 函数方式:
const myPlugin = function (app, options) {
    // 配置全局方法
    app.config.globalProperties.globalMethod = function (value) {
        // 转为小写
        return value.toLowerCase();
    };
    // 注册公共组件
    app.component("Header", Header);
    // 注册公共指令
    app.directive("upper", function (el, binding) {
        console.log("binding:", binding);
        // 转为大写
        el.textContent = binding.value.toUpperCase();
        if (binding.arg === "small") {
            el.style.fontSize = options.small + "px";
        } else if (binding.arg === "medium") {
            el.style.fontSize = options.medium + "px";
        } else {
            el.style.fontSize = options.large + "px";
        }
    });
};

export default myPlugin;

使用插件:

javascript 复制代码
import {createApp} from "vue";
import "../style.css";
import App from "./plugin.vue";
import myPlugin from "./myPlugin/index.js";

const app = createApp(App);
// 安装插件
app.use(myPlugin, {
    small: 12,
    medium: 24,
    large: 36
});
app.mount("#app");
vue 复制代码
<template>
  <Header></Header>
  <p v-upper:large="'hello large'"></p>
  <p v-upper:medium="'hello medium'"></p>
  <p v-upper:small="'hello small'"></p>
  <p>{{ globalMethod("Hello World") }}</p>
</template>

效果:

相关推荐
-许平安-1 天前
MCP项目笔记七(插件 calculator)
c++·笔记·json·plugin·mcp
Grocery store owner2 天前
vue3使用wangeditor上传附件以及添加表格,可以直接复制粘贴excel内容
vue3·wangeditor
floret. 小花2 天前
Vue3 知识点总结 · 2026-03-27
前端·面试·electron·学习笔记·vue3
sam.li3 天前
GhidraMCP 原理与使用部署
ai·逆向·插件·mcp·ghidra
梵得儿SHI4 天前
Vue 3 生态工具实战:UI 组件库与表单验证完全指南
前端·ui·vue3·elementplus·表单验证·antdesignvue·veevalidate
A_nanda4 天前
Vue项目升级
前端·vue3·vue2
sqmw5 天前
MFCMouseEffect:把桌面输入反馈这件事,做成一个真正可扩展的引擎
c++·插件·引擎·鼠标特效·键鼠指示·鼠标伴宠
创梦流浪人5 天前
soli-admin一款开箱即用的RBAC后台项目
java·spring boot·vue3·springsecurity
l1t8 天前
DeepSeek总结的用 C# 构建 DuckDB 插件说明
前端·数据库·c#·插件·duckdb
floret. 小花9 天前
Vue3 + Electron 知识点总结 · 2026-03-21
前端·面试·electron·学习笔记·vue3