首页 > 编程知识 正文

vue源码在哪看,new vue 方法参数

时间:2023-05-04 03:30:59 阅读:11642 作者:3355

上一篇文章介绍了在当前Vue实例不是组件时执行mergeOptions方法的Vue构造函数的一些实现。

虚拟机. $ options=合并选项(resolveconstructoroptions (VM.constructor ),选项||{ }, VM ) mergeoptions方法将来到后续博文今天主要研究resolveConstructorOptions方法,但正如字面所示,该方法是为了分析constructor上的options属性而来的。 看看源代码吧。

exportfunctionresolveconstructoroptions (ctor : class component ) { let options=Ctor.options //具有super属性, 说明Ctor是Vue.extend构建的子类if (ctor.super (constsuperoptions=resolveconstructoroptions (ctor.super ) constcachedsuperoptions ) Vue构造函数上的选项,如directives、filters、 if(superoptions )!==cachedSuperOptions ) { //super option changed, //needtoresolvenewoptions.ctor.super options=super options//checkifthereareanylate-modified/attached options () 440 constmodifiedoptions=resolvemodifiedoptions (ctor )/updatebaseextendoptionsif (修改选项) extend ) ctor.extexted options=ctor.options=merge options (super options,ctor.extendoptions(if ) options.name ) options.components ) oon

Ctor是基本的Vue构造函数。 ctor(ctor其实是构造函数。 )是基本Vue构造函数,例如,使用new关键字创建新Vue构造函数的实例

constVM=newVue(El:'#app ',data: ) message:'HelloChris'} )此时,options是vue构造函数上的选项。 如下图所示

这个选项是在哪里定义的? 在前面的代码中,您似乎看不到options的定义在哪里。 我们应该如何找到这个options定义的位置呢?

在这里,我先告诉你如何找到package.json。 在这里可以找到我们平时使用的npm脚本。 以npm run dev为例。 实际上,npm run dev执行了以下命令

' dev ' : ' roll up-w-cscripts/config.js----环境目标3360 web-full-dev '

rollup是一个类似于web包的打包工具。 可以看到,该命令指向地址scripts/config,然后也指定了Target。 找到script/config并在此文件中输入

有目标为web-full-dev的配置。

//runtimecompilerdevelopmentbuild (browser ) we B- full-dev (3: ) entry : resolve (web/entry-runtime-with dst ) 3360 env: )开发)、alias3360 ) he: )./entity此文件是Vue构造函数的第一个包装。 我今天分析了与光学相关的内容,但是由于此包没有与光学相关的内容,所以不会解压缩此文件(有文章稍后会详细介绍)。 但是请注意这里的代码

. import Vue from './runtime/index ' .我们的Vue构造函数的第二层包位于此文件中

。忽略其他的代码,我们来看关于Vue.options的部分

...import Vue from 'core/index' // 第三层包装import platformDirectives from './directives/index'import platformComponents from './components/index'...// install platform runtime directives & componentsextend(Vue.options.directives, platformDirectives)extend(Vue.options.components, platformComponents)...// platformDirectives相关// 这里导出Vue全局指令model,showimport model from './model'import show from './show'export default { model, show}// platformComponents相关// 这里导出Vue全局组件Transition,TransitionGroupimport Transition from './transition'import TransitionGroup from './transition-group'export default { Transition, TransitionGroup}

上面的代码主要是给Vue.options.directives添加model,show属性,给Vue.options.components添加Transition,TransitionGroup属性。那么还有filters,_base属性,以及components中的KeepAlive又是怎么来的呢?
这就要看Vue的第三层包装里都做了些什么?找到core/index,同样我们只看Vue.options相关代码。

mport Vue from './instance/index'import { initGlobalAPI } from './global-api/index'...initGlobalAPI(Vue)...

instance/index 就是我们第二篇文章——构造函数定义的那个文件。这个文件我们之前看过,没有和Vue构造函数options相关的代码。那么我们剩下的没有配置的options一定是在initGlobalAPI上配置了。接来下看看/global-api/index的代码。

/* @flow */import { ASSET_TYPES } from 'shared/constants'...export function initGlobalAPI (Vue: GlobalAPI) { ... Vue.options = Object.create(null) ASSET_TYPES.forEach(type => { Vue.options[type + 's'] = Object.create(null) }) Vue.options._base = Vue extend(Vue.options.components, builtInComponents) ...}// shared/constants.jsexport const ASSET_TYPES = [ 'component', 'directive', 'filter']// core/components/indeximport KeepAlive from './keep-alive'export default { KeepAlive}

上面这层包装就把filters,_base和components中的KeepAlive都实现了。通过这三层包装,Vue构造函数的options对象就生成了,看这些文字可能有点绕,我们直接上图。


回到resolveConstructorOptions的源码中,当Ctor.super不存在时,直接返回基础构造器的options。即上图经过两次包装的options。那么Ctor.super是什么呢?
Ctor.super是通过Vue.extend构造子类的时候。Vue.extend方法会为Ctor添加一个super属性,指向其父类构造器

Vue.extend = function (extendOptions: Object): Function { ... Sub['super'] = Super ...}

所以当Ctor时基础构造器的时候,resolveConstructorOptions方法返回基础构造器的options。除了Ctor是基础构造器之外,还有一种是Ctor是通过Vue.extend构造的子类。这种情况比较复杂,下一篇文章专门对其进行介绍,敬请期待!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。