博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue源码分析系列之入口文件分析
阅读量:7256 次
发布时间:2019-06-29

本文共 5938 字,大约阅读时间需要 19 分钟。

入口寻找

  1. 入口platforms/web/entry-runtime-with-compiler中import了./runtime/index导出的vue。
  2. ./runtime/index中引入了core/index中的vue.
  3. core/index中引入了instance/index中的vue
  4. instance/index中,定义了vue的构造函数

instance/index中的vue

function Vue (options) {  if (process.env.NODE_ENV !== 'production' &&    !(this instanceof Vue)  ) {    warn('Vue is a constructor and should be called with the `new` keyword')  }  this._init(options)}// 原型上挂载了init方法,用来做初始化initMixin(Vue)// 原型上挂载$data的属性描述符getter,返回this._data// 原型上挂载$props的属性描述符getter, 返回this._props// 原型上挂载$set与$delete方法,用来为对象新增/删除响应式属性// 原型上挂载$watch方法stateMixin(Vue)// 原型上挂载事件相关的方法, $on、$once、$off、$emit。eventsMixin(Vue)// 原型上挂载_update、$destroy与$forceUpdate方法,与组件更新有关。lifecycleMixin(Vue)// 原型挂载组件渲染相关方法,_render方法(用来返回vnode,即虚拟dom)renderMixin(Vue)export default Vue

core/index中的vue

index

import Vue from './instance/index'import { initGlobalAPI } from './global-api/index'import { isServerRendering } from 'core/util/env'// 初始化一些全局APIinitGlobalAPI(Vue)// 是否是服务端渲染Object.defineProperty(Vue.prototype, '$isServer', {  get: isServerRendering // global['process'].env.VUE_ENV === 'server'})// ssr相关Object.defineProperty(Vue.prototype, '$ssrContext', {  get () {    /* istanbul ignore next */    return this.$vnode && this.$vnode.ssrContext  }})Vue.version = '__VERSION__'export default Vue

initGlobalAPI

// 定义vue配置对象,配置对象详情见 import config from '../config'中的备注  const configDef = {}  configDef.get = () => config  Object.defineProperty(Vue, 'config', configDef)  // 定义一些内部公用方法  Vue.util = {    warn, // ⚠️警告打印相关    extend, // 浅拷贝函数    mergeOptions, // 配置合并,用到的时候细看    defineReactive // 定义响应式属性的方法。  }  // 静态方法,同$set、$delete、$nextTick  Vue.set = set  Vue.delete = del  Vue.nextTick = nextTick  Vue.options = Object.create(null)  ASSET_TYPES.forEach(type => {    Vue.options[type + 's'] = Object.create(null)  })  // Vue.options => {"components":{},"directives":{},"filters":{}}  // 跟Weex's multi-instance scenarios多场景有关  Vue.options._base = Vue;  //将内置组件塞进来  extend(Vue.options.components, builtInComponents)  // 定义Vue.use,主要在应用在插件系统中  initUse(Vue)  // 定义Vue.mixin, 就一句this.options = mergeOptions(this.options, mixin)  initMixin(Vue)  // 定义Vue.extend, 用作原型继承,通过它,可以创建子组件的构造函数  initExtend(Vue)  // 扩展Vue.component,Vue.directive,Vue.filter方法  initAssetRegisters(Vue)

runtime/index中的vue

import Vue from 'core/index'import config from 'core/config'import { extend, noop } from 'shared/util'import { mountComponent } from 'core/instance/lifecycle'import { devtools, inBrowser, isChrome } from 'core/util/index'import {  query,  mustUseProp,  isReservedTag,  isReservedAttr,  getTagNamespace,  isUnknownElement} from 'web/util/index'import { patch } from './patch'import platformDirectives from './directives/index'import platformComponents from './components/index'// 一些标签检查类的方法。平台相关Vue.config.mustUseProp = mustUsePropVue.config.isReservedTag = isReservedTagVue.config.isReservedAttr = isReservedAttrVue.config.getTagNamespace = getTagNamespaceVue.config.isUnknownElement = isUnknownElement// 平台相关指令组件// 指令有model与show// 组件有Transition与TransitionGroupextend(Vue.options.directives, platformDirectives)extend(Vue.options.components, platformComponents)// install platform patch functionVue.prototype.__patch__ = inBrowser ? patch : noop// public mount methodVue.prototype.$mount = function (  el?: string | Element,  hydrating?: boolean): Component {  el = el && inBrowser ? query(el) : undefined  return mountComponent(this, el, hydrating)}

entry-runtime-with-compiler中的vue

import config from 'core/config'import { warn, cached } from 'core/util/index'import { mark, measure } from 'core/util/perf'import Vue from './runtime/index'import { query } from './util/index'import { shouldDecodeNewlines } from './util/compat'import { compileToFunctions } from './compiler/index'// 根据id返回dom内容const idToTemplate = cached(id => {  const el = query(id)  return el && el.innerHTML})// 重写$mount方法const mount = Vue.prototype.$mountVue.prototype.$mount = function (  el?: string | Element,  hydrating?: boolean): Component {  el = el && query(el)  /* 将vue绑定到body或者html元素上的错误提示 */  if (el === document.body || el === document.documentElement) {    process.env.NODE_ENV !== 'production' && warn(      `Do not mount Vue to  or  - mount to normal elements instead.`    )    return this  }  const options = this.$options  // 解析template或者el属性,将其转化为render函数  if (!options.render) {    let template = options.template    // 获得模板字符串    if (template) {      if (typeof template === 'string') {        if (template.charAt(0) === '#') {          template = idToTemplate(template)        }      } else if (template.nodeType) {        template = template.innerHTML      } else {        if (process.env.NODE_ENV !== 'production') {          warn('invalid template option:' + template, this)        }        return this      }    } else if (el) {      template = getOuterHTML(el)    }    // 获得模板字符串后,编译模板为render函数    if (template) {      /* istanbul ignore if */      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {        mark('compile')      }      const { render, staticRenderFns } = compileToFunctions(template, {        shouldDecodeNewlines,        delimiters: options.delimiters,        comments: options.comments      }, this)      options.render = render      options.staticRenderFns = staticRenderFns      /* istanbul ignore if */      if (process.env.NODE_ENV !== 'production' && config.performance && mark) {        mark('compile end')        measure(`vue ${this._name} compile`, 'compile', 'compile end')      }    }  }  return mount.call(this, el, hydrating)}/** * Get outerHTML of elements, taking care * of SVG elements in IE as well. */function getOuterHTML (el: Element): string {  if (el.outerHTML) {    return el.outerHTML  } else {    const container = document.createElement('div')    container.appendChild(el.cloneNode(true))    return container.innerHTML  }}Vue.compile = compileToFunctionsexport default Vue

文章链接

转载地址:http://afvdm.baihongyu.com/

你可能感兴趣的文章
对象键
查看>>
[LeetCode] Unique Binary Search Trees, Solution
查看>>
[AtCoder2558]Many Moves
查看>>
【数据库_Mysql】JAVA-数据库Date格式在前台JSP页面的获取
查看>>
程序员的职业发展 (转)
查看>>
mysql中Time Date DateTime TimeStaamp区别
查看>>
STM32是如何进入中断服务函数xxx_IRQHandler的
查看>>
JFinal极速开发框架使用笔记
查看>>
基于jQuery,使用sina ip api,实现异步ip查询
查看>>
业余的工作
查看>>
第一篇博客,希望能坚持下去
查看>>
开发者必知:提升Android应用开发性能的十大要点
查看>>
数据结构总体
查看>>
Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener
查看>>
redis 系列23 哨兵Sentinel (上)
查看>>
软考算法题大观
查看>>
node(redis)
查看>>
Jmeter命令行运行实例讲解
查看>>
vs中正常IIS发布网站后css样式、图片丢失jQuery报错 $ is not defined
查看>>
javascript 时间函数整理
查看>>