Vue.js Render functionsVue.js recommends us to use templates to build HTML. Here, we can use the render function as a closer-to-the-compiler alternative to templates. Vue.js render functions are also used along with Vue.js components. Most of the time, the render function is created by the Vue.js compiler. When you specify a template on your component, this template's content is processed by the Vue.js compiler that will return a render function. The render function essentially returns a virtual DOM node, which will be rendered by Vue.js in your browser DOM. What is the virtual Document Object Model or "DOM"?The virtual DOM allows Vue.js to render your component in its memory before updating your browser. This makes everything faster because it has to do only a few interactions with the browser. When Vue.js updates the browser DOM, it compares the updated virtual DOM to the previous one and updates the real DOM with the modified parts. That's how it enhances performance. Let's take an example of a simple component and see what the render function puts the effect when we use it within the example. Index.html file: Index.js file: Let's use a simple CSS file to make the output more attractive. Index.css file: After the execution of the program, you will see the following output: Output: ![]() Here, you can see that the component shows the above result as Hello Students. Now, if you reuse the component repeatedly, you will see that the result would be printed again and again. For example, Output: ![]() Suppose, you don't want the same text to be printed again and again so, what will you do? Let's make some changes and type something inside the component as follows: Index.html file: Index.js file will be same as above. After the execution of the program, you will see the following output: Output: ![]() You can see that the output remains the same as earlier. It does not change the text as we want. Use of SlotNow, we will use slots that are provided by components to get the desired result. Syntax: See the following example: Index.html file: Index.js file: After the execution of the program, you will see the following output: Output: ![]() How to use render Function?Suppose, you have to change the color and size of the results in output for example, we were using h1 tag in our previous examples and we want to change the HTML tag to p tag or div tag or any other tag for the same component. We have all the flexibility to do the changes by using the render function. Render function helps make the component dynamic and use the way it is required by keeping it common and helping pass arguments using the same component. Let's see an example to demonstrate the use of render function: Index.html file: Index.js file: After the execution of the program, you will see the following output: Output: ![]() Example ExplanationIn the above example, you can see that we have changed the component and added the render function with props property using the following code in the Index.js file: Here, the props property looks like the following code: When we use components with render functions, they do not have a template tag or property. Instead of them, they define a function called render that receives a createElement in the following structure: Syntax: You can see it in the example as follows: We have also defined a property named elementtype, which takes attributes field of type string. In another required field, we have mentioned that the field is mandatory. See the render function code where we have used the elementtype property as follows: The render function gets createElement as the argument and returns the same. The CreateElement creates the DOM element in the same way as it does in JavaScript. We have also split the elementtype on comma, using the values in the attrs field. CreateElement takes the first param as the elementtag to be created. It is passed to the component as follows: The component accepts the props field as the above code. It starts with the symbol : and after that the name of the props is specified. After specifying the prop's name, we have to pass the element tag, color, fontsize, and the id of the element. In the render function, you can see that the first element is the elementtag in createElement field which is given to the createElemet as follows: In the above code, the a[0] is the html element tag. The next parameters are the attributes for the element tag. They are defined in the attr field as follows: Here, we have defined two attributes for the element tag: id tag and style tag.
The message we have given in component is specified as follows: The slots field is used to define the text that we want to print in the createElement using the following code: After the execution of the program when you see the output, you will see that all the output entries are displayed in a specific way. This is because of the elements f the component structure we have defined in a specific way. Event binding in Vue.js render functionsLet's see an example to demonstrate event binding in Vue.js render function. See the following example: Example Index.html file: Index.js file: After the execution of the program, you will see the following output: Output: ![]() When you click on the "Click me" button, you will see that the number of clicks has been displayed as how many times you have clicked on the button. See the output: ![]() In the above example, the button is clicked four times. Next TopicVue.js Reactivity System
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com