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>

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

组件注册完成,输出:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM