You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// src/core/instance/index.jsfunctionVue(options){if(process.env.NODE_ENV!=='production'&&!(thisinstanceofVue)){warn('Vue is a constructor and should be called with the `new` keyword')}this._init(options)}initMixin(Vue)stateMixin(Vue)eventsMixin(Vue)lifecycleMixin(Vue)renderMixin(Vue)exportdefaultVue
// src/core/global-api/index.jsimportconfigfrom'../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'importbuiltInComponentsfrom'../components/index'import{observe}from'core/observer/index'exportfunctioninitGlobalAPI(Vue: GlobalAPI){// configconstconfigDef={}configDef.get=()=>configif(process.env.NODE_ENV!=='production'){configDef.set=()=>{warn('Do not replace the Vue.config object, set individual fields instead.')}}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.Vue.util={
warn,
extend,
mergeOptions,
defineReactive
}Vue.set=setVue.delete=delVue.nextTick=nextTick// 2.6 explicit observable APIVue.observable=<T>(obj: T): T =>{observe(obj)returnobj}
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
extend(Vue.options.components, builtInComponents)
initUse(Vue)
initMixin(Vue)
initExtend(Vue)
initAssetRegisters(Vue)
}
Vue.set=setVue.delete=delVue.nextTick=nextTick// 2.6 explicit observable APIVue.observable=<T>(obj: T): T =>{observe(obj)returnobj}
下面是对 options 属性的创建,后面的 options 合并章节中起着关键的作用。
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
通过上文 Vue 源码学习(一) - 目录结构与构建,我们了解了整个项目的目录结构,这次我们就从构造函数开始,看 vue 到
export default Vue
都经历了什么。构造函数
根据
entry
配置,找到入口文件,根据文件的引用关系找到 Vue 的构造函数所在
最终我们在
src/core/instance/index.js
中找到了 Vue 的构造函数,内容部分也很简单,传入一个options
参数,如果是非生产环境且没用使用new Vue
的方式调用函数,给出警告,然后执行_init
方法。接着经过如下
5个方法的包装处理,最终对外抛出
Vue
。我们后面的分析,也是根据这几个方法的顺序来分析,他们内部到底做了哪些处理。
全局API
在此之前,我们再来看下
通过上一篇文章的目录可知,
global-api
是负责给 Vue 构造函数挂载全局的方法(静态方法)或属性的代码。它对我们后续的代码分析很重要,所以在分析_init
前,我们不妨先看看global-api
具体挂载了哪些方法和属性呢?我们来逐行看下代码,首先是在构造函数 Vue 上增加了一个
config
的静态只读属性,读取config
返回的是core/config.js
中导出的对象,如果试图修改该值时,在非生产环境下会给一个警告。接着是扩展了
util
属性,增加了warn
,extend
,mergeOptions
和defineReactive
四个方法。然后是在 Vue 上增加了
set
,delete
以及nextTick
三个方法,同时在2.6
以上版本,新增了observable
方法,原本必须在一个 Vue 实例中配置的响应对象,现在可以在 Vue 实例外部通过Vue.observable(data)
来创建了。下面是对
options
属性的创建,后面的 options 合并章节中起着关键的作用。首先是创建一个空对象,然后遍历
src/shared/constants.js
中ASSET_TYPES
数组,分创建一个空对象,经过处理后的
options
最终变成:紧接着的这句代码:
把
builtInComponents
返回的值与Vue.options.components
进行合并操作,最终
Vue.options
变为:在文件最后,还有4个 init* 方法,我们再来逐个看下每个方法都是干嘛的。
该方法的作用是在 Vue 构造函数上添加
use
方法,该方法是用来安装 Vue 插件的。该方法是在 Vue 上添加
mixin
方法,该方法是全局注册一个混入,并且会影响注册之后所有创建的每个 Vue 实例。该方法是在 Vue 上添加了
Vue.cid
静态属性和extend
静态方法。该方法继承自 Vue 构造器,创建一个子类。ASSET_TYPES
我们已不陌生,就是上面提到的component
,directive
和filter
。该方法在 Vue 上增加了三个静态方法,他们分别用来全局注册组件,指令和过滤器。我们再回溯到前面的文件,分别扩展了
directives
和components
:小结
至此整个构造函数都讲解完了,主要是通过
global-api/index.js
这个文件,对 Vue 的构造函数增加了许多静态属性和方法,方便后续的使用。此时 Vue 上挂载的属性和方法如下:参考
The text was updated successfully, but these errors were encountered: