vue install 注册组件

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>

说明:以上两个文件位于同一目录

组件注册完成,输出:

猜你喜欢

转载自www.cnblogs.com/mengfangui/p/9046525.html