1、引入自定义的vue-myPlugin.js文件,js文件名称自定义,代码如下:
(function (window) {const MyPlugin = {}MyPlugin.install = function (Vue, options) {Vue.myGlobalMethod = function () {console.log('Vue函数对象的myGlobalMethod()')}Vue.directive('my-directive',function (el, binding) {el.textContent = binding.value.toUpperCase();})Vue.prototype.$myMethod = function () {console.log('vm $myMethod()')}}window.MyPlugin = MyPlugin
})(window)
2、html页面引入自定义的vue-myPlugin.js文件,并且声明使用插件vue-myPlugin.js,代码和图如下:
<!DOCTYPE html>
<html lang&#61;"en">
<head><meta charset&#61;"UTF-8"><title>插件</title>
</head>
<body><div id&#61;"test"><p v-my-directive&#61;"msg"></p>
</div><script type&#61;"text/Javascript" src&#61;"../js/vue.js"></script>
<script type&#61;"text/Javascript" src&#61;"vue-myPlugin.js"></script>
<script type&#61;"text/Javascript">Vue.use(MyPlugin) const vm &#61; new Vue({el: &#39;#test&#39;,data: {msg: &#39;HaHa&#39;}})Vue.myGlobalMethod()vm.$myMethod()new Object()
</script>
</body>
</html>
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/0d80e8a685a9a87b.png?x-oss-process&#61;image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpMTMyNTE2OTAyMQ&#61;&#61;,size_16,color_FFFFFF,t_70)
3、效果图&#xff1a;
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/21e585a7e21fc7dc.png?x-oss-process&#61;image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpMTMyNTE2OTAyMQ&#61;&#61;,size_16,color_FFFFFF,t_70)