Vue之插件
(function (){
    //需要对外暴露的插件对象
    const MyPlugin={}
    MyPlugin.install = function (Vue, options) {
// 1. 添加全局方法或 property
        Vue.myGlobalMethod = function () {
// 逻辑.
            console.log('VUE函数对象的方法myGlobalMethod')
        }
// 2. 添加全局资源
        Vue.directive('my-directive', function (el,binding){
           el.textContent=binding.value.toUpperCase();
        })
// 4. 添加实例方法
        Vue.prototype.$myMethod = function (methodOptions) {
// 逻辑...
            console.log('添加实例方法')
        }
    }
    //向外暴露
    window.MyPlugin=MyPlugin
})()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="test">
<p my-directive="msg"></p>
</div>
<script src="../js/vue.js"></script>
<script src="./vue-muPlugin.js"></script>
<script>
  Vue.use(MyPlugin)
  Vue.myGlobalMethod()
 const vm= new Vue({
    el:'#test',
    data:{
      msg:'I Like You'
    }
  })
  vm.$myMethod()
</script>
</body>
</html>
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/articles/15004698.html
                    
                
                
            
        
浙公网安备 33010602011771号