2018-01-13

Vue  用模块化的方式构建项目


想要提高代码的复用性,vue 项目中可以使用插件的方法:

MyPlugin.install = function (Vue, options) {

// 1. 添加全局方法或属性

    Vue.myGlobalMethod =function(){

        // 逻辑...

      }

// 2. 添加全局资源

Vue.directive('my-directive', {

    bind (el, binding, vnode, oldVnode) {

        // 逻辑...

    }

    ...

  })

// 3. 注入组件

  Vue.mixin({

    created:function(){

        // 逻辑...

    }

    ...

  })

// 4. 添加实例方法

Vue.prototype.$myMethod =function(methodOptions){

    // 逻辑...

  }

}

你可能感兴趣的:(2018-01-13)