美文网首页马文的地下室
学习笔记《Vue 的插件机制》

学习笔记《Vue 的插件机制》

作者: 马文Marvin | 来源:发表于2017-04-23 00:00 被阅读11次

Vue 并没有特别的插件机制,官方文档非常简练:
https://vuejs.org/v2/guide/plugins.html

官方建议了五种为 Vue 添加插件的方式,后面有这些方式的例子:

  1. Add some global methods or properties. e.g. vue-custom-element
  2. Add one or more global assets: directives/filters/transitions etc. e.g. vue-touch
  3. Add some component options by global mixin. e.g. vuex
  4. Add some Vue instance methods by attaching them to Vue.prototype.
  5. A library that provides an API of its own, while at the same time injecting some combination of the above. e.g. vue-router
MyPlugin.install = function (Vue, options) {
  // 1. add global method or property
  Vue.myGlobalMethod = function () {
    // something logic ...
  }
  // 2. add a global asset
  Vue.directive('my-directive', {
    bind (el, binding, vnode, oldVnode) {
      // something logic ...
    }
    ...
  })
  // 3. inject some component options
  Vue.mixin({
    created: function () {
      // something logic ...
    }
    ...
  })
  // 4. add an instance method
  Vue.prototype.$myMethod = function (options) {
    // something logic ...
  }
}

其中第三点中的 global mixin 是 Vue 本身的一种混入机制(类似多重继承),可以混入方法(methods),组件(components),模板指令(directives)等等

参照前面的例子,Vue 的插件是必须有 install() 方法的,之后就可以使用 Vue.use() 的方式调用:

Vue.use(MyPlugin, { someOption: true })

这是一个 Vue 的插件列表(并不完全):
https://github.com/vuejs/awesome-vue#components--libraries

相关文章

  • 学习笔记《Vue 的插件机制》

    Vue 并没有特别的插件机制,官方文档非常简练:https://vuejs.org/v2/guide/plugin...

  • 二、路由安装

    插件 Vue.js 提供插件机制,即Vue.use(plugin) 安装VueRouter,这个插件会调用 plu...

  • Vue 插件编写

    vue插件介绍 2. 插件分类 主要注册与绑定机制如下: export default{install(Vue...

  • Vue 插件编写

    vue插件介绍 2. 插件分类 插件类型 主要注册与绑定机制如下: export default{ instal...

  • 倒计时-vue

    HTML: script: Vue学习笔记-倒计时插件 https://www.jianshu.com/p/3fd...

  • vue框架视频学习第一天笔记

    vue框架视频学习第一天笔记 webpack编译 webpack编译文件基础插件babel-clibabel-co...

  • vue 基础指令以及过滤器

    === 浏览器插件 VS Code插件 玩转Vs code 为什么学习Vue 什么是MVVM Vue初体验 Vue...

  • vue和react的对比

    vue的优点 vue的插件机制 不用像react一层一层得包裹,也不用在每个模块中进行import。 vue有模板...

  • Vue.js - day01

    Vue.js - day01 插件推荐 vue官方推出的插件,高亮,一些提示 vue的代码提示 框架的学习方式 没...

  • Vue插件机制

    插件是实现扩展的重要途径,我们可以根据自己的情况编写一个插件,也可以通过应用程序实例app身上的use方法使用插件...

网友评论

    本文标题:学习笔记《Vue 的插件机制》

    本文链接:https://www.haomeiwen.com/subject/ymrezttx.html