PrefaceIn 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 renderOfficial 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 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 explanationWhen 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 templateSimilarities: The Differences:
Note: template and render cannot be used together, otherwise it will be invalid Rendering ExampleFor 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 } }); }, SummarizeThis 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:
|
<<: HTML uses marquee to achieve text scrolling left and right
>>: Common styles of CSS animation effects animation
01. Overview Absolute paths and relative paths ar...
Table of contents Vite project build optimization...
1: Differences in speed and loading methods The di...
1. Install Docker 1. I installed Centos7 in the v...
FOUC is Flash of Unstyled Content, abbreviated as ...
WML (Wireless Markup Language). It is a markup la...
1. Install vsftpd component Installation command:...
Database stored procedures DROP PROCEDURE IF EXIS...
Preface Recently, I was working on a report funct...
Table of contents Preface LED Trigger Start explo...
Use MySQL proxies_priv (simulated role) to implem...
1. Error reproduction I can access the MySQL data...
When installing packages on an Ubuntu server, you...
question I encountered a problem when writing dat...
Result: html <nav id="nav-1"> <...