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

Introduction to the B-Tree Insertion Process

In the previous article https://www.jb51.net/arti...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Detailed explanation of how to select all child elements using CSS

How to recursively select all child elements usin...

Specific use of the autoindex module in the Nginx Http module series

The main function of the brower module is to dete...

Vue3 gets the current routing address

Correct answer Using useRouter : // router path: ...

JavaScript+html to implement front-end page sliding verification (2)

This article example shares the specific code of ...

VSCode+CMake+Clang+GCC environment construction tutorial under win10

I plan to use C/C++ to implement basic data struc...

Analysis of pitfalls in rounding operation of ROUND function in MySQL

This article uses examples to illustrate the pitf...

Two ways to install the Linux subsystem in Windows 10 (with pictures and text)

Windows 10 now supports Linux subsystem, saying g...

View the number of files in each subfolder of a specified folder in Linux

count script #!/bin/sh numOfArgs=$# if [ $numOfAr...

Detailed installation and configuration of MySql on Mac

1. Download and install Download the community ed...