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

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

Understanding of the synchronous or asynchronous problem of setState in React

Table of contents 1. Is setState synchronous? asy...

Three ways to communicate between Docker containers

We all know that Docker containers are isolated f...

Examples of common Nginx misconfigurations

Table of contents Missing root location Off-By-Sl...

Why TypeScript's Enum is problematic

Table of contents What happened? When to use Cont...

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the I...

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory cd /etc/nginx mkdir ssl ...

js to achieve simple accordion effect

This article shares the specific code of js to ac...

The whole process of realizing website internationalization using Vite2 and Vue3

Table of contents Preface Install vue-i18n Config...

Detailed installation and configuration of hadoop2.7.2 under ubuntu15.10

There are many Hadoop installation tutorials on L...

Web front-end skills summary (personal practical experience)

1. Today, when I was making a page, I encountered ...