Detailed explanation of the use of Vue.js render function

Detailed explanation of the use of Vue.js render function

Vue recommends using templates to create your HTML in most cases. However, in some scenarios, you really need the full programming power of JavaScript, which is the render function, which is closer to a compiler than a template.

At the HTML level, we decided to define the component interface as follows: generate h1-h6 tags by passing different levels 1-6, and use slots to generate content

<div id="div1">
    <child :level="1">Hello world!</child>
</div>

<script type="text/x-template" id="child-template">
  <h1 v-if="level === 1">
    <slot></slot>
  </h1>
  <h2 v-if="level === 2">
    <slot></slot>
  </h2>
  <h3 v-if="level === 3">
    <slot></slot>
  </h3>
  <h4 v-if="level === 4">
    <slot></slot>
  </h4>
  <h5 v-if="level === 5">
    <slot></slot>
  </h5>
  <h6 v-if="level === 6">
    <slot></slot>
  </h6>
</script>

<script type="text/javascript">
    /**
     * Globally register the child component. Note that if the template value starts with #, it is used as an option symbol and the innerHTML of the matching element will be used as the template. A common trick is to use <script type="x-template"> to include the template. The advantage of this is that the HTML will not render the content inside* 
     * Using template here is not the best choice:
     * 1. The code is lengthy* 2. Inserting content in different titles requires repeated use of slots 
     * 3. Since components must have a root element, the title and content are wrapped in a useless div, such as <div><h1>hello world</h1></div>
     */

    Vue.component('child', {
      template: '#child-template',
      props: {
        level:
          type: Number,
          required: true
        }
      },
      data: function() {
        return {
          a: 1
        }
      }
    })

    new Vue({
        el:"#div1"
    })
  </script>

We try to implement the above example using the render function. Note that when using the render function, the template option will be ignored.

createElement receives 3 parameters:

The first parameter can be an HTML tag name, a component or a function; this parameter is required;
The second one is the data object {Object} (optional);

Here is an example:

<div id="div1">
    <child :level="1">
        Hello world!
    </child>
    <child :level="2">
        <!-- will not be displayed -->
        <span slot="footer">this is footer slot</span>
        <p slot="header">this is header slot</p>
    </child>
</div>


<script>
    Vue.component('child', {
        render: function (createElement) {
            console.log(this.$slots);
            return createElement(
                'h' + this.level, // tagName tag name {
                    // Set the class for each h tag
                    'class': {
                        foo: true,
                        bar: false
                    },
                    //Finally rendered as inline style style: {
                        color: 'red',
                        fontSize: '14px'
                    },
                    // Other HTML attributes attrs: {
                        id: 'foo',
                        'data-id': 'bar'
                    },
                    // DOM attributes domProps: {
                        // innerHTML: 'from domProps',
                    },
                    // Event listener based on "on"
                    // So no longer supports modifiers such as v-on:keyup.enter on: {
                        click: this.clickHandler
                    },
                    // ...
                },
                // You can get the static content in the VNodes list from this.$slots // $slots.default is used to access the unnamed slot of the component
                // When you may need a named slot, you need to specify the slot name, this.$slots.header
                // [this.$slots.default,this.$slots.footer,this.$slots.header] //Display level 2 slots [this.$slots.default] //Display only unnamed slots
            )
        },
        template: '<div v-if="level===1"><slot></slot></div>', // will be ignored props: {
            level:
                type: Number,
                required: true
            }
        },
        methods: {
            clickHandler: function () {
                console.log('clickHandler')
            }
        }
    })

    new Vue({
        el: "#div1"
    })
</script>

The rendering is as follows:

Write the picture description here

This is the end of this article about the detailed use of Vue.js's render function. For more relevant Vue.js's render function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • VUE render function usage and detailed explanation
  • Vue Render function creates DOM node code example
  • Vue render function actual combat to implement tabs tab component
  • Detailed explanation of the use of render function in Vue
  • How to use the render function in Vue
  • Understand the use of VUE's render function
  • Why introduce the implementation of render function in Vue

<<:  How to detect file system integrity based on AIDE in Linux

>>:  Solutions to MySQL OOM (memory overflow)

Recommend

How to add double quotes in HTML title

<a href="https://www.jb51.net/" titl...

How to upgrade CentOS7 to CentOS8 (detailed steps)

This article uses a specific example to introduce...

Detailed explanation of CocosCreator Huarongdao digital puzzle

Table of contents Preface text 1. Panel 2. Huaron...

MySql 8.0.11-Winxp64 (free installation version) configuration tutorial

1. Unzip the zip package to the installation dire...

MySQL installation and configuration tutorial for Mac

This article shares the MySQL installation tutori...

js canvas realizes slider verification

This article example shares the specific code of ...

How to pop up a temporary QQ dialog box to chat online without adding friends

In fact, this is very simple. We add an a tag to ...

How to use Docker plugin to remotely deploy projects to cloud servers in IDEA

1. Open port 2375 Edit docker.service vim /lib/sy...

Clever use of webkit-box-reflect to achieve various dynamic effects (summary)

In an article a long time ago, I talked about the...

UrlRewriter caching issues and a series of related explorations

When developing a website function, the session c...