zoukankan      html  css  js  c++  java
  • Vue2.0 render: h => h(App)为什么如此写

    Vue2.0 render: h => h(App)的解释

    render: h => h(App)是ES6的写法,其实就是如下内容的简写:

    render: function (createElement) {
         return createElement(App);
    }
    

    官方文档中是这样的,createElement 是 Vue.js 里面的 函数,这个函数的作用就是生成一个 VNode节点,render 函数得到这个 VNode 节点之后,返回给 Vue.js 的 mount 函数,渲染成真实 DOM 节点,并挂载到根节点上。

    render: function (createElement) {
        return createElement(
          'h' + this.level,   // tag name 标签名称
          this.$slots.default // 子组件中的阵列
        )
      }
    

    然后ES6写法,

    render: createElement => createElement(App)
    

    然后用h代替createElement,使用箭头函数来写:

    render: h => h(App)
    

    好,现在来解释h的涵义,尤雨溪在一个回复中提到:

    It comes from the term "hyperscript", which is commonly used in many virtual-dom implementations. "Hyperscript" itself stands for "script that generates HTML structures" because HTML is the acronym for "hyper-text markup language".
    它来自单词 hyperscript,这个单词通常用在 virtual-dom 的实现中。Hyperscript 本身是指
    生成HTML 结构的 script 脚本,因为 HTML 是 hyper-text markup language 的缩写(超文本标记语言)

    也就是说,createElement 函数是用来生成 HTML DOM 元素的,而上文中的 Hyperscript也是用来创建HTML结构的脚本,这样作者才把 createElement 简写成 h。

    而 createElement(也就是h)是vuejs里的一个函数。这个函数的作用就是生成一个 VNode节点,render 函数得到这个 VNode 节点之后,返回给 Vue.js 的 mount 函数,渲染成真实 DOM 节点,并挂载到根节点上。

    其实在vue 1.0 中,这样的写法也就是如下的含义:

    new Vue({
      el: '#app',
      template:'</App>'
      componets: {App}
    })
    

    然后页面中使用

    <div id='app'>
      <app></app>
    </div>
    
  • 相关阅读:
    Multiple actions were found that match the request Web API
    基于REST架构的Web Service设计
    netbeans常用快捷键
    C#中 字符串转换为计算公式,并计算结果
    简谈asp.net下的异步加载
    简谈回顾多条件搜索查询。(适用于新手,老鸟飘过)
    简单回顾NPOI导入导出excel文件
    sql 中的Bulk和C# 中的SqlBulkCopy批量插入数据 ( 回顾 and 粗谈 )
    扩展lamda表达中distinct按照字段去除重复
    log4Net(写入日志文件)
  • 原文地址:https://www.cnblogs.com/chenxi188/p/13891559.html
Copyright © 2011-2022 走看看