Understanding render in Vue scaffolding

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the new Vue code in the entry file main.js there is a code render: h=>h(App);

insert image description here

This code is not like the code we usually use when using vue. I will write the general Vue code

	import Acomponent from "../Acomponent"
	vm = new Vue({
		el:"#app"
		data(){
			return {
				a:"aaa",
				b:"bbb"
			}
		},
		template:`<div>
			<span>this is a test</span>
			<Acomponent></Acomponent>
		</div>`,
		components:{
			Acomponent
		}
	})

The above code is the code we can understand normally. There is a template, and other components can be introduced in the template. But why is there a render method in the scaffolding?

According to our own ideas, we can change the scaffolding code to see

insert image description here

Start the scaffolding, npm run serve and check the result. An error is reported. The information is as follows

insert image description here

So, we can say that the Vue introduced by the scaffolding is a Vue without a template parser. If you want to parse the template, you need to use the render function to help you go to the node_modules folder of the project to see which Vue we have introduced.

import Vue from 'vue' 

insert image description here

We open the vue/dist file and see a lot of files, as shown in the figure

insert image description here


The error message says that we have two ways to solve it, one is to introduce the complete vue.js and the other is to use render.
Let's import the full version and see if it can solve the problem.

insert image description here

Let's look at the second one, introducing Vue without a template parser, using render,
Let's talk about a render first
Render is a function with one parameter, which is used to create a node.

insert image description here
insert image description here

Through console.log, we can see that the parameter createElement is also a function, which creates a VNode object

insert image description here

Next we use arrow functions to simplify render

	render(createElement){
		return createElement("h1","123");
	}
	//Use arrow functions to simplify render:(createElement)=>{return createElement("h1","123")}
	// There is only one parameter in the arrow function, so you don't need to write parentheses. If there is only one line in the method body, you don't need to write curly braces, and you don't need to write return for the return value.
	//So the above code can be simplified to this render:createElemnet=>crementElement("h1","123")
	//Similarly, createElement is originally customized, we can also change the name render: h=>h("h1","123")
	//This is very similar to the code in the scaffolding. In the h function, if it is a native HTML tag, it is written like this. If it is a Vue component, it can be passed directly. All we have is render:h=>h(App)

This is how render is written

Let's talk about why the scaffolding introduces an incomplete Vue for use. We know that after the Vue code is completed, it needs to be packaged, and the core code of Vue is indispensable. After we package it, we don't need to parse the template again. Then, the template parser in the core code of Vue is not needed at all. Therefore, in order to reduce the size of the code, Vue removed the template parser, but when we develop, we need to use it again, so we create a render method to parse the template.
In short, its purpose is to make the packaged code as small as possible.

This is the end of this article about understanding render in Vue scaffolding. For more relevant Vue render understanding 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:
  • The difference between Vue rendering methods render and template
  • Detailed explanation of the use of render function in Vue
  • Detailed explanation of Vue.js render method
  • How to replace template in Vue framework render method

<<:  Summary of commonly used performance test scripts for VPS servers

>>:  Why the explain command may modify MySQL data

Recommend

Summary of new usage examples of computed in Vue3

The use of computed in vue3. Since vue3 is compat...

A brief talk about JavaScript parasitic composition inheritance

Composition inheritance Combination inheritance i...

Why do code standards require SQL statements not to have too many joins?

Free points Interviewer : Have you ever used Linu...

Implementing a simple timer based on Vue method

Vue's simple timer is for your reference. The...

Detailed explanation of the implementation of nginx process lock

Table of contents 1. The role of nginx process lo...

A brief analysis of different ways to configure static IP addresses in RHEL8

While working on a Linux server, assigning static...

The most commonly used HTML tags to create web pages

1. Optimization of commonly used HTML tags HTML s...

Ubuntu regularly executes Python script example code

Original link: https://vien.tech/article/157 Pref...

How to uninstall and reinstall Tomcat (with pictures and text)

Uninstall tomcat9 1. Since the installation of To...

Example of implementing colored progress bar animation using CSS3

Brief Tutorial This is a CSS3 color progress bar ...

Use iframe to submit form without refreshing the page

So we introduce an embedding framework to solve th...

JavaScript quickly implements calendar effects

This article example shares the specific code of ...