VUE render function usage and detailed explanation

VUE render function usage and detailed explanation

Preface

In normal programming, most of the time, HTML is created through templates. However, in some special cases, the template method cannot meet the needs well. At this time, JavaScript programming skills are needed to operate. At this point, it's time for the render function to show its strength.

The role of render

Official website example entry

In this example on the official website, components are used to put the same content into h1-h6 tags through solt. When using the traditional method, the code is not only lengthy, but also <slot></slot> is repeatedly written in each level of the title, and it needs to be repeated again when the anchor element is to be inserted. After using the render function, the code is much simpler.

Vue.component('anchored-heading', {
  render: function (createElement) {
    return createElement(
      'h' + this.level, // tag name this.$slots.default // child node array)
  },
  props: {
    level:
      type: Number,
      required: true
    }
  }
})

The role of the render function is to greatly simplify the code when the code implemented by template in the scene is lengthy, cumbersome and has a lot of repetition.

Render function explanation

When using the render function, a parameter createElement is used. This createElement parameter is essentially also a function, which is the tool used to build the virtual DOM in Vue. Let's take a look at createElement.

In the createelement method, there are three parameters:

return createEement(, {}, [])

1. The first parameter (required parameter): is mainly used to provide HTML content in DOM. The type can be string, object or function.

2. The second parameter (object type, optional): used to set some styles, attributes, parameters of the passed components, binding events, etc. in this DOM.

3. The third parameter (type is array, array element type is VNode, optional): mainly used to set the distribution content, such as other newly added components.

Note: All vnodes in the component tree must be unique

By passing in the createElement parameter, a virtual node is created, and then the node is returned to render.

In general, the essence of the render function is to create a virtual node.

The difference between render and template

Similarities:

The render function, like template , creates an HTML template

Differences:

  • Template is suitable for simple logic, and render is suitable for complex logic.
  • template is relatively easy to understand for users, but lacks flexibility; the custom render function is highly flexible, but has higher requirements for users.
  • The performance of render is higher, and the performance of template is lower.
  • There is no compilation process when using the render function for rendering, which is equivalent to the user directly giving the code to the program. Therefore, using it requires high user requirements and is prone to errors.
  • The priority of Render function is higher than that of the template, but please note that the Mustache (double curly braces) syntax cannot be used again.

Note: template and render cannot be used together, otherwise it will be invalid

Rendering Example

For example, if you encapsulate a set of common button components at one time, the buttons have four styles (success, error, warning, and default).

The template method is as follows:

 <div class="btn btn-success" v-if="type === 'success'">{{ text }}</div>
 <div class="btn btn-danger" v-else-if="type === 'danger'">{{ text }}</div>
 <div class="btn btn-warning" v-else-if="type === 'warning'">{{ text }}</div>

This is fine when there are few buttons, but once the number of buttons increases, it will become very lengthy. At this time, a render function is needed.

Generate button DOM according to the situation

Before using the render function, you need to remove the template tag and only keep the logic layer.

The class is dynamically filled in through the passed type, and the content is added to the DOM through inderText.

render(h) {
  return h('div', {
   class: {
    btn: true,
    'btn-success': this.type === 'success',
    'btn-danger': this.type === 'danger',
    'btn-warning': this.type === 'warning'
   },
   domProps: {
    innerText: this.text
   },
   on: {
    click: this.handleClick
   }
  });
 },

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of the use of Vue.js render function
  • 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

<<:  HTML uses marquee to achieve text scrolling left and right

>>:  Common styles of CSS animation effects animation

Recommend

Use of Linux relative and absolute paths

01. Overview Absolute paths and relative paths ar...

Vite2.0 Pitfalls

Table of contents Vite project build optimization...

The difference between div and table in speed, loading, web application, etc.

1: Differences in speed and loading methods The di...

Tutorial on deploying the open source project Tcloud with Docker on CentOS8

1. Install Docker 1. I installed Centos7 in the v...

Solution to web page confusion caused by web page FOUC problem

FOUC is Flash of Unstyled Content, abbreviated as ...

What is WML?

WML (Wireless Markup Language). It is a markup la...

Detailed explanation of FTP environment configuration solution (vsftpd)

1. Install vsftpd component Installation command:...

Implementation steps of Mysql merge results and horizontal splicing fields

Preface Recently, I was working on a report funct...

Explore how an LED can get you started with the Linux kernel

Table of contents Preface LED Trigger Start explo...

Mysql 5.7.18 Using MySQL proxies_priv to implement similar user group management

Use MySQL proxies_priv (simulated role) to implem...

Solution to MySQL unable to read table error (MySQL 1018 error)

1. Error reproduction I can access the MySQL data...

Detailed explanation of the update command for software (library) under Linux

When installing packages on an Ubuntu server, you...

Problems encountered in the execution order of AND and OR in SQL statements

question I encountered a problem when writing dat...

CSS3 to achieve menu hover effect

Result: html <nav id="nav-1"> <...