# Vue3 中创建 plugin

# 1. 示例

import { App, Plugin } from 'vue';

export const MyPlugin: Plugin = {
  install(app: App, options: MyOptions) {
    // 全局属性
    app.config.globalProperties.$msg = 'hello world'

    // 全局组件
    app.component('HelloWorld', HelloWorld);

    // 全局指令
    app.directive('focus', {
      mounted(el) {
        el.focus();
      },
    });
  }
}

# 2. 参考

本章目录