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