vue-cli: render:h => h(App)是什么意思

import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),    //生成template(APP)
}).$mount('#app')    //作用域是'#app'

1、render方法的实质就是生成template模板(在#app的作用域里)

2、render是vue2.x新增的一个函数, 这个函数的形参是h

3、vue调用render方法时, 会传入一个createElement函数作为参数(也就是h的实参是createElement函数)

4、createElement用于在页面中创建元素

5、createElement以App为参数进行调用, 即把App的内容创建到页面中的'#app'作用域里

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

等价于

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

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

示例

<!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>

结果

猜你喜欢

转载自www.cnblogs.com/qq254980080/p/10321288.html