在學習vue.js時,使用vue-cli建立了一個vue項目,main.js文件中有一行代碼不知道什麼意思。在網上搜索獲得以下解答:javascript
參考一:https://www.cnblogs.com/longying2008/p/6408753.htmlhtml
參考二:https://www.cnblogs.com/whkl-m/p/6970859.htmlvue
main.js文件內容java
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的經常使用技巧之一。vue-cli
言歸正傳:app
render: h => h(App) 這是是一個箭頭函數是確定的,那對應的es5形式是怎樣的呢???
以下:ide
{ render: h => h(App) }
等價於:函數
{ render: h =>{ return h(App) } }
等價於:學習
{ render: function(h) { return h(App); } }
即:ui
{ render: function(createElement) { return createElement(App); } }
其實看了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>