Vue学习:render函数

在main的js中

new Vue({
    el: '#root',
    template: `
    <App></App>
    `,
    components:{App},
});

You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

出现该问题:给了两个解决办法render函数和完整版本vue

import Vue from 'vue'

使用上面引入的是不完整的: "module": "dist/vue.runtime.esm.js",是精简版本的,缺少了模板解析器,就无法解析template(esm:ES6模块化)

下面这个版本的是完全版本的

引入完整版本的就可以了

/*
该文件是整个项目的入口文件
*/
//引入vue
import Vue from 'vue/dist/vue.js'

//关闭生产提示
Vue.config.productionTip = false

new Vue({
    el: '#root',
    template: `
   <h1>你好</h1>
    `,
});

使用render

/*
该文件是整个项目的入口文件
*/
//引入vue
import Vue from 'vue'
/
//关闭生产提示
Vue.config.productionTip = false

new Vue({
    el: '#root',

  render(creatElement){
    //creatElement函数可以创建具体元素
    console.log(typeof creatElement)
    return creatElement('h1','你好啊')
  }
});
  render(creatElement){
    //creatElement函数可以创建具体元素
    console.log(typeof creatElement)
    return creatElement('h1','你好啊')
  }

精简 没有用到this箭头函数 然后一个参数可以省略 里面只有依据 且是返回值

  render:creatElement=>creatElement('h1','你好啊')

使用元素替代creatElement

 render: h => h('h1','你好啊') //原因在于内置的元素 +内容
   //使用了组件 具体的内容在组件中配置了
    render: h => h(App),//将App组件放入容器中

Vue完整版本:核心(生命周期等)+模板解析器(1/3)==>使用webpack打包,很大,模板解析已经不需要了,已经解析完成了

解析标签里面的模板可以被vue-template-compiler": "^2.6.14"解析.vue里面的模板标签的

在浏览器中使用精简的js:vue.runtime.js

关于不同版本的Vue

1、vue.js和vue.runtime.js 的区别

vue.js是完整版本的js包含核心功能+模板解析器

vue.runtime.xxx.js是运行版本的js.包含核心功能 没有模板解析器

2、因为vue.runtime.xxx.js没有模板解析器,所以不能使用template配置项,

需要使用render函数接收到的creatElement函数去指定具体内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值