成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Vue源碼解析(1)-模版渲染

k00baa / 1019人閱讀

摘要:首先在里調(diào)用函數(shù)把獲取相應(yīng)傳遞過(guò)去里在原型上定義方法對(duì)瀏覽器的怪癖做兼容布爾值。對(duì)瀏覽器的怪癖做兼容布爾值。

首先在init.js里調(diào)用$mount函數(shù)
把el #app獲取相應(yīng)dom傳遞過(guò)去

Vue.prototype._init = function (options) {
   ...
   if (vm.$options.el) {
      vm.$mount(vm.$options.el)
    }   
}

entry-runtime-with-compiler.js里在Vue原型上定義$mount方法

Vue.prototype.$mount = function (
  el,
  hydrating
) { 
       let template = options.template
       template = idToTemplate(template)
       const { render, staticRenderFns } = compileToFunctions(template , {
        shouldDecodeNewlines, // 對(duì)瀏覽器的怪癖做兼容,布爾值。
        shouldDecodeNewlinesForHref, // 對(duì)瀏覽器的怪癖做兼容,布爾值。
        delimiters: options.delimiters, //vue透?jìng)?        comments: options.comments //vue透?jìng)?      }, this)
 } 

template(64)和 render(72), staticRenderFns(73) 對(duì)應(yīng)圖中所示

render是渲染函數(shù),staticrenderfns是靜態(tài)樹(shù)的渲染函數(shù)

//render
function anonymous(
) {
with(this){return _c("div",[_c("h1",{staticStyle:{"color":"red"}},[_v("我是選項(xiàng)模板3")]),_v(" "),_c("p",[_v(_s(number))]),_v(" "),_c("p",[_v(_s(message))]),_v(" "),_m(0)])}
}  
//staticrenderfns
function anonymous(
) {
with(this){return _c("div",[_c("div",[_v("
          1
          "),_c("div",[_v("11")]),_v(" "),_c("div",[_v("12")])]),_v(" "),_c("div",[_v("2")]),_v(" "),_c("div",[_v("3")]),_v(" "),_c("div",[_v("4")]),_v(" "),_c("div",[_v("5")])])}
}
//template

children多的放進(jìn)了staticrenderfns 其余的放進(jìn)了render 至于為什么這么做,傳送門(mén)
從ast到render過(guò)程

compileToFunctions來(lái)源
platform文件下 compiler文件 index.js文件

const { compile, compileToFunctions } = createCompiler(baseOptions)

createCompiler來(lái)源
core文件下 compiler文件 index.js文件

export const createCompiler = createCompilerCreator(function baseCompile (
  template,
  options
) {
  // 使用 parse 函數(shù)將模板解析為 AST
  const ast = parse(template.trim(), options)
 
  // ast對(duì)象進(jìn)行優(yōu)化,找出ast對(duì)象中所有靜態(tài)子樹(shù)
  if (options.optimize !== false) {
    optimize(ast, options)
  }
  // 根據(jù)給定的AST生成最終的目標(biāo)平臺(tái)的代碼
  const code = generate(ast, options)
  return {
    ast,
    render: code.render,
    staticRenderFns: code.staticRenderFns
  }
})

createCompilerCreator來(lái)源
creact-compiler.js下

export function createCompilerCreator (baseCompile) {
  return function createCompiler (baseOptions) {
    function compile (
      template,
      options
    ) {
      const finalOptions = Object.create(baseOptions)
    // 執(zhí)行createCompilerCreator里傳來(lái)的函數(shù) 生生ast等
      const compiled = baseCompile(template, finalOptions)
      compiled.errors = errors
      compiled.tips = tips
      return compiled
    }

    return {
      compile,
      compileToFunctions: createCompileToFunctionFn(compile)
    }
  }
}

compiled如下即

 return {
    ast,
    render: code.render,
    staticRenderFns: code.staticRenderFns
  }
compiled.errors = errors
compiled.tips = tips

總之,在$mount原型函數(shù)里面$mount
給 vue.options 掛載了 render和staticRenderFns
options.render = render
options.staticRenderFns = staticRenderFns

打印 options

掛在后需要渲染

lifecycle.js

mountComponent(){
   callHook(vm, "beforeMount")
   updateComponent = () => {
      vm._update(vm._render(), hydrating)
   }
   // 把更新組件加入依賴
   new Watcher(vm, updateComponent, noop, {
    before () {
      if (vm._isMounted) {
        callHook(vm, "beforeUpdate")
      }
    }
  }, true /* isRenderWatcher */)
}

vm._render()生成傳說(shuō)中的VNode,即虛擬dom

vm._render()執(zhí)行函數(shù)結(jié)果

vm._update方法的實(shí)現(xiàn)

 Vue.prototype._update = function (vnode, hydrating) {
   // 判斷vnode是否初始化過(guò)
    if (!prevVnode) {
      // initial render
      vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */)
    } else {
      // updates
      vm.$el = vm.__patch__(prevVnode, vnode)
    }
  }

通過(guò)__patch__更新

在 platform/web/runtime/index.js文件里執(zhí)行了mountComponent方法

import { mountComponent } from "core/instance/lifecycle"


Vue.prototype.$mount = function (
  el,
  hydrating
) {
  el = el && inBrowser ? query(el) : undefined
  // 調(diào)用mountComponent方法
  return mountComponent(this, el, hydrating)
}

最終效果圖

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/97347.html

相關(guān)文章

  • vue源碼閱讀之?dāng)?shù)據(jù)渲染過(guò)程

    摘要:圖在中應(yīng)用三數(shù)據(jù)渲染過(guò)程數(shù)據(jù)綁定實(shí)現(xiàn)邏輯本節(jié)正式分析從到數(shù)據(jù)渲染到頁(yè)面的過(guò)程,在中定義了一個(gè)的構(gòu)造函數(shù)。一、概述 vue已是目前國(guó)內(nèi)前端web端三分天下之一,也是工作中主要技術(shù)棧之一。在日常使用中知其然也好奇著所以然,因此嘗試閱讀vue源碼并進(jìn)行總結(jié)。本文旨在梳理初始化頁(yè)面時(shí)data中的數(shù)據(jù)是如何渲染到頁(yè)面上的。本文將帶著這個(gè)疑問(wèn)一點(diǎn)點(diǎn)追究vue的思路??傮w來(lái)說(shuō)vue模版渲染大致流程如圖1所...

    AlphaGooo 評(píng)論0 收藏0
  • Vue源碼解析(一)-模版渲染

    摘要:接下來(lái)我們分析下是如何調(diào)用的,這就涉及到了經(jīng)典的機(jī)制此處先簡(jiǎn)單介紹,下一篇會(huì)有較詳細(xì)的分析。 Vue demo 先給出vue簡(jiǎn)單的使用demo,通過(guò)創(chuàng)建一個(gè)Vue的實(shí)例,將被替換成template模版中的內(nèi)容,a,b的值也會(huì)被換成data屬性的值 var vm = new Vue({ el: #app, template: ` {{a}} {} ...

    KitorinZero 評(píng)論0 收藏0
  • Vue源碼解析模版字符串轉(zhuǎn)AST語(yǔ)法樹(shù)

    摘要:通過(guò)對(duì)源碼閱讀,想寫(xiě)一寫(xiě)自己的理解,能力有限故從尤大佬第一次提交開(kāi)始讀,準(zhǔn)備陸續(xù)寫(xiě)模版字符串轉(zhuǎn)語(yǔ)法樹(shù)語(yǔ)法樹(shù)轉(zhuǎn)函數(shù)雙向綁定原理虛擬比較原理其中包含自己的理解和源碼的分析,盡量通俗易懂由于是的最早提交,所以和最新版本有很多差異,后續(xù)將陸續(xù)補(bǔ)充, 通過(guò)對(duì) Vue2.0 源碼閱讀,想寫(xiě)一寫(xiě)自己的理解,能力有限故從尤大佬2016.4.11第一次提交開(kāi)始讀,準(zhǔn)備陸續(xù)寫(xiě): 模版字符串轉(zhuǎn)AST語(yǔ)法...

    chengjianhua 評(píng)論0 收藏0
  • Vue源碼解析模版字符串轉(zhuǎn)AST語(yǔ)法樹(shù)

    摘要:通過(guò)對(duì)源碼閱讀,想寫(xiě)一寫(xiě)自己的理解,能力有限故從尤大佬第一次提交開(kāi)始讀,準(zhǔn)備陸續(xù)寫(xiě)模版字符串轉(zhuǎn)語(yǔ)法樹(shù)語(yǔ)法樹(shù)轉(zhuǎn)函數(shù)雙向綁定原理虛擬比較原理其中包含自己的理解和源碼的分析,盡量通俗易懂由于是的最早提交,所以和最新版本有很多差異,后續(xù)將陸續(xù)補(bǔ)充, 通過(guò)對(duì) Vue2.0 源碼閱讀,想寫(xiě)一寫(xiě)自己的理解,能力有限故從尤大佬2016.4.11第一次提交開(kāi)始讀,準(zhǔn)備陸續(xù)寫(xiě): 模版字符串轉(zhuǎn)AST語(yǔ)法...

    王偉廷 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<