摘要:本次分析的版本是。持续更新中。。。目录的引入的实例化的引入这一章将会分析用户在引入后,框架做的初始化工作创建这个类,并往类上添加类属性类方法和实例属性实例方法。
背景
Vue.js是现在国内比较火的前端框架,希望通过接下来的一系列文章,能够帮助大家更好的了解Vue.js的实现原理。本次分析的版本是Vue.js2.5.16。(持续更新中。。。)
目录Vue.js的引入
Vue的实例化
Vue.js的引入这一章将会分析用户在引入Vue.js后,Vue框架做的初始化工作:创建Vue这个类,并往Vue类上添加类属性&类方法和实例属性&实例方法。
流程图:
流程分析:
1)入口文件(platforms/web/entry-runtime-with-compiler.js)
引入 platforms/web/runtime/index.js 得到Vue类
缓存Vue的原型链上添加$mount方法,并重写该方法
2)platforms/web/runtime/index.js
引入 core/index.js 得到Vue类
往Vue类的config属性上添加mustUseProp,isReservedTag,isReservedAttr,getTagNamespace,isUnknownElement
扩展Vue类options属性的directives,components
给Vue类添加实例方法__patch__,$mount
3)core/index.js
引入core/instance/index.js得到Vue类
为Vue类添加添加全局API
设置Vue实例属性$isServer,$ssrContext
设置Vue类属性 FunctionalRenderContext
添加Vue类的版本号
4)core/instance/index.js
声明Vue类
将Vue类传入各种初始化方法initMixin,stateMixin,eventsMixin,lifecycleMixin,renderMixin
源码分析:
我们将根据上述的流程分析从后往前分析,逐步分析Vue从定义到最后初始化结束的整个流程。
core/instance/index.js
import { initMixin } from "./init"
import { stateMixin } from "./state"
import { renderMixin } from "./render"
import { eventsMixin } from "./events"
import { lifecycleMixin } from "./lifecycle"
import { warn } from "../util/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)
}
// 将Vue类传入各种初始化方法
// 为Vue添加_init实例方法 Vue.prototype._init = function(){}
initMixin(Vue)
// 通过Object.defineProperty方法,添加vue的实例属性$data,$props,主要跟数据相关
// 添加Vue的实例方法 $set,$delete,$watch, eg:Vue.prototype.$set = function(){}
stateMixin(Vue)
// 添加Vue实例基础的事件方法
// 添加Vue实例方法 $on, $off, $emit, $once eg:Vue.prototype.$on = function () {}
eventsMixin(Vue)
// 添加Vue实例生命周期的方法,主要涉及到组件的更新与销毁
// 添加Vue实例方法 $_update,$forceUpdate, $destroy
lifecycleMixin(Vue)
// 添加Vue实例方法 $nextTick, $_render以及_o,_n,_s,_l,_t等组件渲染相关的方法
renderMixin(Vue)
export default Vue
core/index.js
import Vue from "./instance/index"
import { initGlobalAPI } from "./global-api/index"
import { isServerRendering } from "core/util/env"
import { FunctionalRenderContext } from "core/vdom/create-functional-component"
// 为Vue添加类方法
// 通过Object.defineProperty方法添加Vue.config属性,
// 添加Vue.util,Vue.set,Vue.delelt,Vue.delete,Vue.nextTick,Vue.options
// 添加Vue.options上的"components","directives","filters"方法
// 实现Vue.options.components => 内建组件{keep-alive} => Vue.options.components.KeepAlive = xxxx
// 添加Vue.options上的_base属性
// 添加Vue.use,用于VUe插件的安装
// 添加Vue.mixin
// 添加Vue.extend,用于类的继承
// 添加Vue类上"component","directive","filter"方法
initGlobalAPI(Vue)
Object.defineProperty(Vue.prototype, "$isServer", {
get: isServerRendering
})
Object.defineProperty(Vue.prototype, "$ssrContext", {
get () {
/* istanbul ignore next */
return this.$vnode && this.$vnode.ssrContext
}
})
// expose FunctionalRenderContext for ssr runtime helper installation
Object.defineProperty(Vue, "FunctionalRenderContext", {
value: FunctionalRenderContext
})
Vue.version = "__VERSION__"
export default Vue
platforms/web/runtime/index.js
/* @flow */
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,isReservedTag,isReservedAttr,getTagNamespace,isUnknownElement方法
Vue.config.mustUseProp = mustUseProp
Vue.config.isReservedTag = isReservedTag
Vue.config.isReservedAttr = isReservedAttr
Vue.config.getTagNamespace = getTagNamespace
Vue.config.isUnknownElement = isUnknownElement
// 实现Vue.options上的directives,components方法
// Vue.options.directives的model,show
// Vue.options.components的Transition,TransitionGroup方法
extend(Vue.options.directives, platformDirectives)
extend(Vue.options.components, platformComponents)
// install platform patch function
// Vue实例上的__patch__方法
Vue.prototype.__patch__ = inBrowser ? patch : noop
// public mount method
// Vue实例上的$mount方法
Vue.prototype.$mount = function (
el?: string | Element,
hydrating?: boolean
): Component {
el = el && inBrowser ? query(el) : undefined
return mountComponent(this, el, hydrating)
}
// devtools global hook
/* istanbul ignore next */
if (inBrowser) {
setTimeout(() => {
if (config.devtools) {
if (devtools) {
devtools.emit("init", Vue)
} else if (
process.env.NODE_ENV !== "production" &&
process.env.NODE_ENV !== "test" &&
isChrome
) {
console[console.info ? "info" : "log"](
"Download the Vue Devtools extension for a better development experience:
" +
"https://github.com/vuejs/vue-devtools"
)
}
}
if (process.env.NODE_ENV !== "production" &&
process.env.NODE_ENV !== "test" &&
config.productionTip !== false &&
typeof console !== "undefined"
) {
console[console.info ? "info" : "log"](
`You are running Vue in development mode.
` +
`Make sure to turn on production mode when deploying for production.
` +
`See more tips at https://vuejs.org/guide/deployment.html`
)
}
}, 0)
}
export default Vue
platforms/web/entry-runtime-with-compiler.js
/* @flow */
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 { compileToFunctions } from "./compiler/index"
import { shouldDecodeNewlines, shouldDecodeNewlinesForHref } from "./util/compat"
// 实现通过id来缓存模板的功能。
const idToTemplate = cached(id => {
const el = query(id)
return el && el.innerHTML
})
// 缓存mount方法
const mount = Vue.prototype.$mount
// 重新实现Vue实例上的$mount方法
Vue.prototype.$mount = function (
el?: string | Element,
hydrating?: boolean
): Component {
el = el && query(el)
/* istanbul ignore if */
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
// resolve template/el and convert to render function
if (!options.render) {
let template = options.template
if (template) {
if (typeof template === "string") {
if (template.charAt(0) === "#") {
template = idToTemplate(template)
/* istanbul ignore if */
if (process.env.NODE_ENV !== "production" && !template) {
warn(
`Template element not found or is empty: ${options.template}`,
this
)
}
}
} 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)
}
if (template) {
/* istanbul ignore if */
if (process.env.NODE_ENV !== "production" && config.performance && mark) {
mark("compile")
}
const { render, staticRenderFns } = compileToFunctions(template, {
shouldDecodeNewlines,
shouldDecodeNewlinesForHref,
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方法
Vue.compile = compileToFunctions
export default Vue
以上就是引入Vue.js之后整个初始化过程。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/93883.html
摘要:本次分析的版本是。的实例化由上一章我们了解了类的定义,本章主要分析用户实例化类之后,框架内部做了具体的工作。所以我们先看看的构造函数里面定义了什么方法。这个文件声明了类的构造函数,构造函数中直接调用了实例方法来初始化的实例,并传入参数。 背景 Vue.js是现在国内比较火的前端框架,希望通过接下来的一系列文章,能够帮助大家更好的了解Vue.js的实现原理。本次分析的版本是Vue.js2...
摘要:但没办法,还是得继续。因为这边返回的是一个,所以会执行如下代码然后回到刚才的里面,,额,好吧。。。 这段时间折腾了一个vue的日期选择的组件,为了达成我一贯的使用舒服优先原则,我决定使用directive来实现,但是通过这个实现有一个难点就是我如何把时间选择的组件插入到dom中,所以问题来了,我是不是又要看Vue的源码? vue2.0即将到来,改了一大堆,Fragment没了,所以vu...
摘要:写在前面其实最开始不是特意来研究的源码,只是想了解下的命令,如果想要了解命令的话,那么绕不开写的。通过分析发现与相比,变化太大了,通过引入插件系统,可以让开发者利用其暴露的对项目进行扩展。 showImg(https://segmentfault.com/img/bVboijb?w=1600&h=1094); 写在前面 其实最开始不是特意来研究 vue-cli 的源码,只是想了解下 n...
摘要:一方面是因为想要克服自己的惰性,另一方面也是想重新温故一遍。一共分成了个基础部分,后续还会继续记录。文章中如果有笔误或者不正确的解释,也欢迎批评指正,共同进步。最后地址部分源码 Why? 网上现有的Vue源码解析文章一搜一大批,但是为什么我还要去做这样的事情呢?因为觉得纸上得来终觉浅,绝知此事要躬行。 然后平时的项目也主要是Vue,在使用Vue的过程中,也对其一些约定产生了一些疑问,可...
摘要:一方面是因为想要克服自己的惰性,另一方面也是想重新温故一遍。一共分成了个基础部分,后续还会继续记录。文章中如果有笔误或者不正确的解释,也欢迎批评指正,共同进步。最后地址部分源码 Why? 网上现有的Vue源码解析文章一搜一大批,但是为什么我还要去做这样的事情呢?因为觉得纸上得来终觉浅,绝知此事要躬行。 然后平时的项目也主要是Vue,在使用Vue的过程中,也对其一些约定产生了一些疑问,可...
阅读 3388·2021-11-25 09:43
阅读 3699·2021-11-11 16:54
阅读 1060·2021-11-02 14:42
阅读 3999·2021-09-30 09:58
阅读 3932·2021-09-29 09:44
阅读 1525·2019-08-30 15:56
阅读 2384·2019-08-30 15:54
阅读 3180·2019-08-30 15:43