首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【Vue3 从入门到实战 进阶式掌握完整知识体系】027-Vue中的高级语法:更加底层的render函数

【Vue3 从入门到实战 进阶式掌握完整知识体系】027-Vue中的高级语法:更加底层的render函数

作者头像
訾博ZiBo
发布2025-01-06 14:17:46
发布2025-01-06 14:17:46
20400
代码可运行
举报
运行总次数:0
代码可运行

4、更加底层的render函数

通过render函数实现显示不同的h标签
代码语言:javascript
代码运行次数:0
运行
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>

  const app = Vue.createApp({
    template: `
      <my-title :level="3">
        hello
      </my-title>
    `
  });

  // 不同的参数呈现不同的标签
  app.component("my-title", {
    props: ['level'],
    // 使用 render
    render(){
      const { h } = Vue;
      // 匿名插槽 this.$slots.default()
      return h('h' + this.level, {}, this.$slots.default());
      // return h('h' + this.level, {}, 'hello world');
    }
    // 使用 template 写起来很复杂
    // template: `
    //     <h1 v-if="level === 1"><slot /></h1>
    //     <h2 v-if="level === 2"><slot /></h2>
    //     <h3 v-if="level === 3"><slot /></h3>
    //     <h4 v-if="level === 4"><slot /></h4>
    //     <h5 v-if="level === 5"><slot /></h5>
    // `
  });

  const vm = app.mount('#root');
</script>

</html>
运行结果
image-20210614122150396.png
image-20210614122150396.png
render函数和template对比
代码语言:javascript
代码运行次数:0
运行
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>

  const app = Vue.createApp({
    template: `
      <my-title :level="3">
        hello
      </my-title>
    `
  });

  // 不同的参数呈现不同的标签
  app.component("my-title", {
    props: ['level'],
    // 真实 DOM
    // <div>hello</div>
    // render函数 -> 虚拟 DOM (是对真实 DOM 的映射)
    // {
    //   tagName: 'div',
    //   text: 'hello',
    //   attributes: {}
    // }
    // 使用 render 的好处
    // 1、使得 vue 的性能更快;
    // 2、使得 vue 跨平台能力更强;
    // 使用 render
    render(){
      const { h } = Vue;
      // 匿名插槽 this.$slots.default()
      // 虚拟 DOM 
      // tagName:'h' + this.level
      // attributes:{}
      // text:this.$slots.default()
      // vue 会将虚拟 DOM 转换成真实的 DOM ,就显示在了页面上
      return h('h' + this.level, {}, this.$slots.default());
      // return h('h' + this.level, {}, 'hello world');
    }
    // 使用 template 写起来很复杂
    // template: `
    //     <h1 v-if="level === 1"><slot /></h1>
    //     <h2 v-if="level === 2"><slot /></h2>
    //     <h3 v-if="level === 3"><slot /></h3>
    //     <h4 v-if="level === 4"><slot /></h4>
    //     <h5 v-if="level === 5"><slot /></h5>
    // `
  });

  const vm = app.mount('#root');
</script>

</html>
执行逻辑

template模板 ——> render函数 ——> h 系列标签——> 虚拟DOM(JS对象)——> 真实DOM ——> 展示到页面;

无限嵌套render
代码语言:javascript
代码运行次数:0
运行
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>

  const app = Vue.createApp({
    template: `
      <my-title :level="3">
        hello
      </my-title>
    `
  });

  // 不同的参数呈现不同的标签
  app.component("my-title", {
    props: ['level'],
    render(){
      const { h } = Vue;
      return h('h' + this.level, {}, [
        this.$slots.default(),
        h("div", {}, [
          "我是div内容-嵌套1",
          h("h1", {style:"color:red;"}, "我是h1内容-嵌套2")
        ])
      ]);
    }
  });

  const vm = app.mount('#root');
</script>

</html>
运行结果
image-20210614124033834.png
image-20210614124033834.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 4、更加底层的render函数
    • 通过render函数实现显示不同的h标签
    • 运行结果
    • render函数和template对比
    • 执行逻辑
    • 无限嵌套render
    • 运行结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档