vue大部分情况下都是推荐使用template来创建html,但在一些场景中,如果需要JavaScript的完全编程的能力,就需要使用render函数,可理解为render函数比template更接近编译器。
1.render最常见的就是main.js文件中的全局vue引入
import vue from 'vue'
import App from './App.vue'
new Vue({
render: h=>h(App)
// 上面的h只是一个形参,在执行render时会自动传入实参
//h(App)会返回一个虚拟dom
//render是一个配置项,它的作用是指定渲染函数,效果等同于template,用来决定组件的内容
//上一句代码是下面这段代码的缩写
// render: function(h){
// const rs = h(App) //vue实例的内容由App这个文件决定
// console.log(rs)
// return rs
// }
}).$mount('#app')
// #app是./public/index.html中的#app
2.$mount作用:
把上面产生的vue实例挂载到一个id名为#app的dom元素上
让该容器在#app所在的index.html文件内部生效
**new Vue({}).$mount('#app') 等价于 new Vue({el: '#app'})**