1. 程式人生 > >render:h => h(App) 是什麽意思?

render:h => h(App) 是什麽意思?

javascrip isa tar 需要 guid 使用 變量 js文件 cnblogs

在學習vue.js時,使用vue-cli創建了一個vue項目,main.js文件中有一行代碼不知道什麽意思。在網上搜索得到如下解答:

參考一:https://www.cnblogs.com/longying2008/p/6408753.html

參考二:https://www.cnblogs.com/whkl-m/p/6970859.html

main.js文件內容

import Vue from ‘vue‘
import App from ‘./App‘

Vue.config.productionTip = false    // 設置false,已阻止vue在啟動時生成生產提示

/* eslint-disable no-new */ 
new Vue({
  el:‘#app‘,
  render: h => h(App)    
})

 註:/* eslint-disable no-new */這不是一句註釋,在js裏面,new一個對象,需要賦值給某個值(變量),用Vue實例化時,不需要賦值給某值(變量),所以需要單獨給配一條規則,給new Vue這行代碼上面加這個註釋,把這行代碼規則的校驗跳過,通過eslint-disable。是eslint的常用技巧之一。

言歸正傳:

render: h => h(App)   這是是一個箭頭函數是肯定的,那對應的es5形式是怎樣的呢???

  如下:

{
   render: h => h(App)
}

  等價於:

{
   render: h =>{
        return h(App)
    } 
}

  等價於:

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

  即:

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

  createElement 參數

其實看了createElement的官方文檔,我還是不明白createElement的用法。createElement方法的參數有幾個?各個參數的含義、類型是什麽?

例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
    <script type="text/javascript" src="https://unpkg.com/vue"></script>
    <script type="text/javascript">
        var app = new Vue({
            el: ‘#app‘, // 提供一個在頁面上已經存在的 DOM 元素作為 Vue 實例掛載目標
            render: function (createElement) {
                return createElement(‘h2‘, ‘Hello Vue!‘);
            }
        });
    </script>
</body>
</html>

  

render:h => h(App) 是什麽意思?