【重學前端框架】初探Vue中的虛擬DOM

前言

React、Vue中都使用了虛擬DOM,Vue中如何使用的呢?html

Vue中模版轉化成真實DOM的思想

Vue中模版的編譯是以下過程:模版--->ast(抽象樹)-->render 函數->虛擬 dom->實際 dom。vue

vue中的模版經過 compiler 編譯成ast(用於表示模版的 js 對象,也能夠說ast就是一個用來表示源代碼的js對象),而後將ast生成對應render函數(這裏先不談關於ast的轉化細節),render函數而後生成虛擬節點 vnode(用來描述節點及其子節點的信息),vnode的集合組成Virtual Dom(vue組件創建起來的整個vnode樹叫虛擬Dom樹),最後生成真實Dom。node

Vue中模版轉化成虛擬DOM細節

ast的結構

ast 是一種表示源代碼的 js 對象(描述 dom 結構的 js 對象)。react

ast格式:git

function add(a, b) {
    return a +
      // 有什麼奇怪的東西混進來了
      b
  }

轉化成ast後:github

FunctionDeclaration {
  type: 'FunctionDeclaration',
  id: Identifier {
    type: 'Identifier',
    name: 'add',
    loc: {
      start: [Object],
      end: [Object],
      lines: [Lines],
      tokens: [Array],
      indent: 2
    }
  },
  params: [
    Identifier { type: 'Identifier', name: 'a', loc: [Object] },
    Identifier { type: 'Identifier', name: 'b', loc: [Object] }
  ],
  body: BlockStatement {
    type: 'BlockStatement',
    body: [ [ReturnStatement] ],
    loc: {
      start: [Object],
      end: [Object],
      lines: [Lines],
      tokens: [Array],
      indent: 2
    }
  },
  generator: false,
  expression: false,
  async: false,
  loc: {
    start: { line: 2, column: 2, token: 0 },
    end: { line: 6, column: 3, token: 13 },
    lines: Lines {
      infos: [Array],
      mappings: [],
      cachedSourceMap: null,
      cachedTabWidth: undefined,
      length: 7,
      name: null
    },
    tokens: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ],
    indent: 2
  }
}

ast轉化成render函數

vue模版以下:express

<h1>{{ blogTitle }}</h1>

最終會被轉化成以下render函數:segmentfault

render: function (createElement) { 
  return createElement('h1', this.blogTitle) 
}

createElement函數參數:數組

createElement(
  // {String | Object | Function}
  // 一個 HTML 標籤名、組件選項對象,或者
  // resolve 了上述任何一種的一個 async 函數。必填項。
  'div',

  // {Object}
  // 一個與模板中屬性對應的數據對象。可選。
  {
    // (詳情見下一節)
  },

  // {String | Array}
  // 子級虛擬節點 (VNodes),由 `createElement()` 構建而成,
  // 也能夠使用字符串來生成「文本虛擬節點」。可選。
  [
    '先寫一些文字',
    createElement('h1', '一則頭條'),
    createElement(MyComponent, {
      props: {
        someProp: 'foobar'
      }
    })
  ]
)

createElement 第二個參數是節點的屬性:app

{
  // 與 `v-bind:class` 的 API 相同,
  // 接受一個字符串、對象或字符串和對象組成的數組
  'class': {
    foo: true,
    bar: false
  },
  // 與 `v-bind:style` 的 API 相同,
  // 接受一個字符串、對象,或對象組成的數組
  style: {
    color: 'red',
    fontSize: '14px'
  },
  // 普通的 HTML 特性
  attrs: {
    id: 'foo'
  },
  // 組件 prop
  props: {
    myProp: 'bar'
  },
  // DOM 屬性
  domProps: {
    innerHTML: 'baz'
  },
  // 事件監聽器在 `on` 屬性內,
  // 但再也不支持如 `v-on:keyup.enter` 這樣的修飾器。
  // 須要在處理函數中手動檢查 keyCode。
  on: {
    click: this.clickHandler
  },
  // 僅用於組件,用於監聽原生事件,而不是組件內部使用
  // `vm.$emit` 觸發的事件。
  nativeOn: {
    click: this.nativeClickHandler
  },
  // 自定義指令。注意,你沒法對 `binding` 中的 `oldValue`
  // 賦值,由於 Vue 已經自動爲你進行了同步。
  directives: [
    {
      name: 'my-custom-directive',
      value: '2',
      expression: '1 + 1',
      arg: 'foo',
      modifiers: {
        bar: true
      }
    }
  ],
  // 做用域插槽的格式爲
  // { name: props => VNode | Array<VNode> }
  scopedSlots: {
    default: props => createElement('span', props.text)
  },
  // 若是組件是其它組件的子組件,需爲插槽指定名稱
  slot: 'name-of-slot',
  // 其它特殊頂層屬性
  key: 'myKey',
  ref: 'myRef',
  // 若是你在渲染函數中給多個元素都應用了相同的 ref 名,
  // 那麼 `$refs.myRef` 會變成一個數組。
  refInFor: true
}

vue中render函數的應用:

render編寫靈活性更強的組件

createElement( 
'anchored-heading',
{ props: { level: 1 } }, 
[ createElement('span', 'Hello'),' world!'] 
)
<anchored-heading :level="1">  
<span>Hello</span> world! 
<anchored-heading>

Virtual Dom結構

render函數最終生成虛擬DOM。
Virtual Dom只是一個簡單的JS對象,最少包含tag、props和children三個屬性。

Virtual Dom的格式:

{
    tag: "div",
    props: {},
    children: [
        "Hello World", 
        {
            tag: "ul",
            props: {},
            children: [{
                tag: "li",
                props: {
                    id: 1,
                    class: "li-1"
                },
                children: ["第", 1]
            }]
        }
    ]
}

對比React中的虛擬DOM的生成

在React中模版渲染就是定義在render函數中,如下模版:

class Hello extends Component {
  render() {
    return <div>Hello ConardLi</div>;
  }
}

最終會被Babel轉化成調用createElement的形式,而後生成虛擬DOM,這種形式也能夠看成第二種生成模版的方式:

class Hello extends Component {
  render() {
    return React.createElement('div', null, `Hello ConardLi`);
  }
}

在Vue中也能夠像React同樣使用JSX來建立模版,須要引入對應的Bable插件(https://github.com/vuejs/jsx)

import AnchoredHeading from  './AnchoredHeading.vue'
new Vue({ el: '#demo', 
render: function (h) { 
return ( 
    <AnchoredHeading level={1}> 
    <span>Hello</span> 
    world!
    </AnchoredHeading> 
)} 
})

參考資料:
渲染函數&JSX
【React深刻】深刻分析虛擬DOM的渲染原理和特性

相關文章
相關標籤/搜索