1、myPlugin.js文件
let MyPlugin = {}; MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或屬性
Vue.myGlobalMethod = function () { // 邏輯...
console.log('myGlobalMethod') } // 2. 添加全局資源
Vue.directive('my-directive', { bind (el, binding, vnode, oldVnode) { // 邏輯...
} }) // 3. 注入組件
Vue.mixin({ created: function () { // 邏輯...
} }) // 4. 添加實例方法
Vue.prototype.$myMethod = function (methodOptions) { // 邏輯...
console.log('myMethod') } } export default MyPlugin
2、APP.vue文件
<template>
</template>
<script> import Vue from 'vue' import MyPlugin from './myPlugin' Vue.use(MyPlugin) export default { data() { return { } }, methods: { }, mounted: function(){ this.$myMethod(); Vue.myGlobalMethod(); } } </script>
說明:以上兩個文件位於同一目錄
組件注冊完成,輸出: