01 参数

{Object | Function} plugin

02 用法

安装 Vue.js 插件。如果插件是一个对象,必须提供 install 方法。如果插件是一个函数,它会被作为 install 方法。install 方法调用时,会将 Vue 作为参数传入。
该方法需要在调用 new Vue() 之前被调用。
当 install 方法被同一个插件多次调用,插件将只会被安装一次。

03 参考

  1. MyPlugin.install = function (Vue, options) {
  2. // 1. 添加全局方法或 property
  3. Vue.myGlobalMethod = function () {
  4. // 逻辑...
  5. }
  6. // 2. 添加全局资源
  7. Vue.directive('my-directive', {
  8. bind (el, binding, vnode, oldVnode) {
  9. // 逻辑...
  10. }
  11. ...
  12. })
  13. // 3. 注入组件选项
  14. Vue.mixin({
  15. created: function () {
  16. // 逻辑...
  17. }
  18. ...
  19. })
  20. // 4. 添加实例方法
  21. Vue.prototype.$myMethod = function (methodOptions) {
  22. // 逻辑...
  23. }
  24. }


04 源码

我们可以从源码入手分析一下,基于vue 2.6.11 版本,源码地址为:src/core/global-api/use.js

  1. export function initUse (Vue: GlobalAPI) {
  2. // 接受一个plugin参数,限制为 Function | Object两种类型
  3. Vue.use = function (plugin: Function | Object) {
  4. // _installedPlugins 存储所有注册过的 plugin
  5. const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
  6. // 保存注册组件的数组,不存在则创建,存在则直接返回,不允许重复注册
  7. if (installedPlugins.indexOf(plugin) > -1) {
  8. return this
  9. }
  10. // additional parameters
  11. // 将传入的参数转换成数组
  12. const args = toArray(arguments, 1)
  13. // 将Vue对象拼接到数组头部
  14. args.unshift(this)
  15. // 如果提供了 install 方法,则直接调用
  16. if (typeof plugin.install === 'function') {
  17. // 如果组件是对象,且提供install方法,调用install方法将参数数组传入,改变`this`指针为该组件
  18. plugin.install.apply(plugin, args)
  19. } else if (typeof plugin === 'function') {
  20. // 否则直接执行
  21. plugin.apply(null, args)
  22. }
  23. // 将plugin存储到 installedPlugins,表示y已经注册过
  24. installedPlugins.push(plugin)
  25. return this
  26. }
  27. }
  28. /**
  29. * Convert an Array-like object to a real Array.
  30. */
  31. export function toArray (list: any, start?: number): Array<any> {
  32. start = start || 0
  33. let i = list.length - start
  34. const ret: Array<any> = new Array(i)
  35. while (i--) {
  36. ret[i] = list[i + start]
  37. }
  38. return ret
  39. }

基于 vue-router3.1.6 版本,源码位置: src/install.js

  1. import View from './components/view'
  2. import Link from './components/link'
  3. /*
  4. export 一个 Vue 引用,在打包的时候不希望把 Vue作为依赖包打进去,但是又希望可以使用 Vue提供的一些方法,
  5. */
  6. export let _Vue
  7. // Vue.use安装插件时候需要暴露的install方法
  8. export function install (Vue) {
  9. // 判断是否已安装过
  10. if (install.installed && _Vue === Vue) return
  11. install.installed = true
  12. _Vue = Vue
  13. const isDef = v => v !== undefined
  14. const registerInstance = (vm, callVal) => {
  15. let i = vm.$options._parentVnode
  16. if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
  17. i(vm, callVal)
  18. }
  19. }
  20. // 混入 beforeCreate
  21. Vue.mixin({
  22. beforeCreate () {
  23. // 在option上面存在router则代表是根组件
  24. if (isDef(this.$options.router)) {
  25. this._routerRoot = this
  26. // 根组件的_router属性为,new Vue传进去的router
  27. this._router = this.$options.router
  28. // 执行 init方法
  29. this._router.init(this)
  30. // 通过defineReactive方法,来把this._router.history.current变成响应式的,这个方法的底层就是object.defineProperty
  31. Vue.util.defineReactive(this, '_route', this._router.history.current)
  32. } else {
  33. // 如果该组件不是根组件,那么递归往上找,直到找到根组件的。
  34. this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
  35. }
  36. registerInstance(this, this)
  37. },
  38. destroyed () {
  39. registerInstance(this)
  40. }
  41. })
  42. /*
  43. 下面通过给 Vue.prototype定义$router、$route属性后,所有的Vue实例(组件)都可以直接访问到
  44. */
  45. // 设置代理,访问 this.$router 时直接代理到 this._routerRoot._router
  46. Object.defineProperty(Vue.prototype, '$router', {
  47. get () { return this._routerRoot._router }
  48. })
  49. // 设置代理,访问 this.$route 时直接代理到 this._routerRoot._route
  50. Object.defineProperty(Vue.prototype, '$route', {
  51. get () { return this._routerRoot._route }
  52. })
  53. // 注册 router-view 和 router-link 组件
  54. Vue.component('RouterView', View)
  55. Vue.component('RouterLink', Link)
  56. const strats = Vue.config.optionMergeStrategies
  57. // use the same hook merging strategy for route hooks
  58. strats.beforeRouteEnter = strats.beforeRouteLeave = strats.beforeRouteUpdate = strats.created
  59. }
  60. // 组件的注册
  61. const install = function(Vue, opts = {}) {
  62. locale.use(opts.locale);
  63. locale.i18n(opts.i18n);
  64. components.forEach(component => {
  65. Vue.component(component.name, component);
  66. });
  67. Vue.use(InfiniteScroll);
  68. Vue.use(Loading.directive);
  69. Vue.prototype.$ELEMENT = {
  70. size: opts.size || '',
  71. zIndex: opts.zIndex || 2000
  72. };
  73. Vue.prototype.$loading = Loading.service;
  74. Vue.prototype.$msgbox = MessageBox;
  75. Vue.prototype.$alert = MessageBox.alert;
  76. Vue.prototype.$confirm = MessageBox.confirm;
  77. Vue.prototype.$prompt = MessageBox.prompt;
  78. Vue.prototype.$notify = Notification;
  79. Vue.prototype.$message = Message;
  80. };
  81. /* istanbul ignore if */
  82. if (typeof window !== 'undefined' && window.Vue) {
  83. install(window.Vue);
  84. }

vue-router中的install方法主要做了以下几件事:

  1. 通过 mixin混入的方式,如果是根组件,则直接把根组件的 _router 设置为 this.$options.router
  2. 如果不是根组件,那么递归往上找,直到找到根组件的,使用_routerRoot标记
  3. 通过给 Vue.prototype定义$router$route属性后,使得所有的Vue实例(组件)都可以直接访问到 $router$route 属性
  4. 注册<router-link><router-view>组件