百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> vue源码架构的解读(具体)
分享文章到:

vue源码架构的解读(具体)

发布时间:09/01 来源:未知 浏览: 关键词:

本篇文章给大家带来的内容是关于vue源码架构的解读(具体),有必然的参照 价值,有需要的伴侣可以参照 一下,但愿对你有所帮忙。

下载

去github上下载Vue

npm install 
npm run dev

运转起来

rollup + flow

vue使用使用rollup打包,flow标准数据类型

rollup可以先用webpack套用,读起来差不多,时间有限,究竟只要5分钟,这个就不消去看rollup文档了

入口

翻开package.json
我们看scripts配置

 "dev": "rollup -w -c scripts/config.js --environment TARGET:web-full-dev",
  "dev:cjs": "rollup -w -c scripts/config.js --environment TARGET:web-runtime-cjs-dev",

寻到scripts/config.js
翻开

按照配置TARGET的不一样会选中不一样的config

同时在这里配置了process.env.NODE_ENV 环境

TARGET有CommonJS,ES Modules,UMD关于js引入类型的
还有weex,ssr

'web-runtime-cjs-dev': {
    entry: resolve('web/entry-runtime.js'),
    dest: resolve('dist/vue.runtime.common.dev.js'),
    format: 'cjs',
    env: 'development',
    banner
  }

在alias.js下设定了别号途径
我们先介绍src/platforms

里面有web和weex 离别的web和weex入口

在web文件下是CommonJS,ES Modules,UMD关于js引入类型,server的打包入口

翻开web/entry-runtime.js
引入

import Vue from './runtime/index'

export default Vue

翻开./runtime/index

import Vue from 'core/index'

Vue.prototype.$mount = function (
  el?: string | Element,
  hydrating?: boolean
): Component {
  el = el && inBrowser ? query(el) : undefined
  return mountComponent(this, el, hydrating)
}
export default Vue

在vue原型上增加了mount办法
处置了devtools,没有安置提示安置devtools

给了这句提醒dev环境提醒

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

platforms名目夹讲解完毕

core名目

翻开core/instance/index
映入面前的是

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)
}

initMixin(Vue)
stateMixin(Vue)
eventsMixin(Vue)
lifecycleMixin(Vue)
renderMixin(Vue)

export default Vue

先施行的是initMixin(Vue)

翻开init

export function initMixin (Vue) {
  Vue.prototype._init = function (options?: Object) {
    const vm = this
    // a uid 
    vm._uid = uid++
    
    let startTag, endTag
    /* istanbul ignore if */
    if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
      startTag = `vue-perf-start:${vm._uid}`
      endTag = `vue-perf-end:${vm._uid}`
      mark(startTag)
    }

    // a flag to avoid this being observed
    vm._isVue = true
    // 处置传入的options
    // merge options
    if (options && options._isComponent) {
      // optimize internal component instantiation
      // since dynamic options merging is pretty slow, and none of the
      // internal component options needs special treatment.
      initInternalComponent(vm, options)
    } else {
       // 传入的options,默许的options一起合并挂载到vm.$options上
      vm.$options = mergeOptions(
        resolveConstructorOptions(vm.constructor),
        options || {},
        vm
      )
    }
    /* istanbul ignore else */
    if (process.env.NODE_ENV !== 'production') {
      // 代理
      initProxy(vm)
    } else {
      vm._renderProxy = vm
    }
    // 生命周期
    initLifecycle(vm)
     // emit on 事件
    initEvents(vm)
    // 处置render vdom
    initRender(vm)
    callHook(vm, 'beforeCreate')
    // 处置Injections
    initInjections(vm) // resolve injections before data/props
    // 双向数据绑定,监听订阅
    initState(vm)
    initProvide(vm) // resolve provide after data/props
    callHook(vm, 'created')
    
    /* istanbul ignore if */
    if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
      vm._name = formatComponentName(vm, false)
      mark(endTag)
      measure(`vue ${vm._name} init`, startTag, endTag)
    }
    // 渲染到dom
    if (vm.$options.el) {
      vm.$mount(vm.$options.el)
    }
  }
}

lifecycle

翻开 lifecycle

export function callHook (vm: Component, hook: string) {
  // disable dep collection when invoking lifecycle hooks
  pushTarget()
  //施行对象的周期函数,周期函数最后被处置成数组
  const handlers = vm.$options[hook]
  const info = `${hook} hook`
  if (handlers) {
    for (let i = 0, j = handlers.length; i < j; i++) {
      invokeWithErrorHandling(handlers[i], vm, null, vm, info)
    }
  }
  if (vm._hasHookEvent) {
    vm.$emit('hook:' + hook)
  }
  popTarget()

callHook 的时候,是施行响应周期,开发者在周期函数里所写的

Events

initEvents实现了 emit on 等办法,请参照 监听者订阅者模式,这里不详解

render

renderMixin函数

增加了 $nextTick _render 原型对象

$nextTick会在dom跟新后马上调取

nextTick(fn, this)是一个自施行函数

_render返回的是node的js数据,还不是dom

做了Vdom

initRender函数

给vm增加了_c和 $createElement用来渲染的办法

state

if (!(key in vm)) {
      proxy(vm, `_props`, key)
    }

给vue属性做代理,拜访this.a可以得到this.data.a 的值

export function initState (vm: Component) {
  vm._watchers = []
  const opts = vm.$options
  if (opts.props) initProps(vm, opts.props)
  if (opts.methods) initMethods(vm, opts.methods)
  if (opts.data) {
    initData(vm)
  } else {
    observe(vm._data = {}, true /* asRootData */)
  }
  if (opts.computed) initComputed(vm, opts.computed)
  if (opts.watch && opts.watch !== nativeWatch) {
    initWatch(vm, opts.watch)
  }
}

给数据做监听

stateMixin函数

增加原型对象

 Vue.prototype.$set = set
 Vue.prototype.$delete = del

其他

src/compiler 做了编译处置

core/componetd 做了keep-alive

core/util 封装了通用办法

core/vdom vdom算法

以上团体架构剖析完毕

附张图
255774865-5c481ebcb5dc7_articlex.jpg

以上就是vue源码架构的解读(具体)的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有151人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板