欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue 初始化-静态成员

程序员文章站 2022-03-19 15:31:02
地址:vue-dev\src\core\global-api\index.js源码地址: https://github.com/vuejs/vue/* @flow */import config from '../config'import { initUse } from './use'import { initMixin } from './mixin'import { initExtend } from './extend'import { initAssetRegisters } f...

地址:vue-dev\src\core\global-api\index.js
源码地址: https://github.com/vuejs/vue

/* @flow */

import config from '../config'
import { initUse } from './use'
import { initMixin } from './mixin'
import { initExtend } from './extend'
import { initAssetRegisters } from './assets'
import { set, del } from '../observer/index'
import { ASSET_TYPES } from 'shared/constants'
import builtInComponents from '../components/index'
import { observe } from 'core/observer/index'

import {
  warn,
  extend,
  nextTick,
  mergeOptions,
  defineReactive
} from '../util/index'

export function initGlobalAPI (Vue: GlobalAPI) {
  // config
  const configDef = {}
  configDef.get = () => config
  if (process.env.NODE_ENV !== 'production') {
    configDef.set = () => {
      warn(
        'Do not replace the Vue.config object, set individual fields instead.'
      )
    }
  }
  // 初始化config 对象
  Object.defineProperty(Vue, 'config', configDef)

  // exposed util methods.
  // NOTE: these are not considered part of the public API - avoid relying on
  // them unless you are aware of the risk.
  // 这些工具方法不视作全局API的一部分
  Vue.util = {
    warn,
    extend,
    mergeOptions,
    defineReactive
  }
  // 静态方法 set del nextTick
  Vue.set = set
  Vue.delete = del
  Vue.nextTick = nextTick

  // 2.6 explicit observable API
  //  让一个对象可响应
  Vue.observable = <T>(obj: T): T => {
    observe(obj)
    return obj
  }
  // 初始化 vue.options 对象,并拓展 components/directives/filters
  Vue.options = Object.create(null)
  ASSET_TYPES.forEach(type => {
    Vue.options[type + 's'] = Object.create(null)
  })

  // this is used to identify the "base" constructor to extend all plain-object
  // components with in Weex's multi-instance scenarios.
  Vue.options._base = Vue
  // 设置keep-alive组件 extend的作用是浅拷贝,将一个属性拷贝到另外的对象中for in遍历
  extend(Vue.options.components, builtInComponents)
  // 注册Vue.set
  initUse(Vue)
  // 注册Vue.mixin
  initMixin(Vue)
  // 注册Vue.extend()  基于传入的options返回一个组件的构造函数
  initExtend(Vue)
  // 注册Vue.directive 、 vue.component() 、 Vue.filter() 
  initAssetRegisters(Vue)
}

本文地址:https://blog.csdn.net/qq_38074118/article/details/111995771