Detailed explanation of template tag usage (including summary of usage in Vue)

Detailed explanation of template tag usage (including summary of usage in Vue)

1. Template tag in HTML5

The content in the template tag in HTML will not be displayed on the page. However, when viewing the page DOM structure in the background, there is template tag. This is because the template tag is inherently invisible, it sets display:none; attribute.

<!--The current page only displays the content "I am a custom expression abc", not "I am a template", because the template tag is inherently invisible-->
<template><div>I am template</div></template>
<abc>I am custom expression abc</abc>

2. Properties and methods of template tag operation

  • content attribute: In js, the DOM object corresponding to the template tag has a content attribute, and the corresponding attribute value is a DOM node, and the nodeName of the node is #document-fragment. This property can be used to obtain the content in the template tag. template對象.content can call the getElementById, querySelector, and querySelectorAll methods to obtain the child nodes inside.
  • innerHTML : can get the HTML in the template tag.
<template id="template">
 <div id="div1">I am template</div>
 <div>I am template</div>
</template>
<script>
 let o = document.getElementById("tem");
 console.log(o.content.nodeName);//#document-fragment
 console.log(o.content.querySelectorAll("div"));//NodeList(2) [div#div1, div]. Get a class array console.log(o.content.getElementById("div1"));//<div id="div1">I am template</div>
 console.log(o.innerHTML);//'<div id="div1">I am template</div><div>I am template</div>'
</script>

3. Template in Vue

1. The template tag is inside the element bound to the vue instance

It can display the content in the template tag, but the template tag does not exist when checking the background DOM structure. If the template tag is not placed inside the element bound to the Vue instance, the content inside cannot be displayed on the page by default, but the template tag exists in the background DOM structure.

<div id="app">
 <!--The content in the template tag here is displayed and the template tag does not exist in the DOM-->
 <template>
 <div>I am template</div>
 <div>I am template</div>
 </template>
</div>
<!--The content in the template tag here is not displayed on the page, but the tag and its internal structure exist in the DOM structure-->
<template id="template">
 <div id="div1">I am template</div>
 <div>I am template</div>
</template>
<script src="node_modules/vue/dist/vue.js"></script>
<script>
 let vm = new Vue({
 el: "#app",
 });
</script>

Note: The template tag inside the element bound to the vue instance does not support the v-show directive, that is, v-show="false" does not work for the template tag. However, the template tag now supports v-if, v-else-if, v-else, and v-for instructions.

<div id="app">
 <template v-if="true">
 <!--At this time, the content in the template tag is displayed on the page, but the DOM structure does not have a template tag-->
 <div>I am template</div>
 <div>I am template</div>
 </template>
 <div v-if="true">
 <!--The content in the div tag is displayed on the page, and the DOM structure has the outermost div tag-->
 <div>I am template</div>
 <div>I am template</div>
 </div>
 <!--6 'I am template' will be output here and there is no template tag in the DOM structure-->
 <template v-for="a in 3">
 <div>I am template</div>
 <div>I am template</div>
 </template>
</div>
<script src="node_modules/vue/dist/vue.js"></script>
<script>
 let vm = new Vue({
 el: "#app",
 });
</script>

2. Template property in Vue instance

Compile the template attribute value in the instance, and replace the element bound to the Vue instance with the compiled DOM. If there is content in the element bound to the Vue instance, the content will be directly overwritten.
Features:

1) If there is a template attribute in the Vue instance, the attribute value will be compiled, and the compiled virtual DOM will directly replace the element bound to the Vue instance (that is, the element bound to el);
2) The DOM structure in the template attribute can only have one root element. If there are multiple root elements, you need to use v-if, v-else, and v-else-if to set it to display only one of the root elements;
3) The data defined in the Vue instance data and methods can be used in the attribute value corresponding to this attribute.
<!--This page displays hello-->
<div id="app"></div>
<!--The template tag here must be defined outside the element bound to vue, and the content in the following template tag will not be displayed on the page-->
<template id="first">
 <div v-if="flag">{{msg}}<div>
 <div v-else>111<div>
</template>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
 let vm = new Vue({
 el:"#app",
 data:{
  msg:"hello",
  flag:true
 },
 template:"#first" //This property can be used to replace all the content in the custom template property with the content of the app, and will overwrite the original content in it, and there is no template tag when viewing the DOM structure});
</script>

In the above example, the template tag in HTML can be changed into a custom tag as follows. However, the following method can also replace the app element with the content in the <abc></abc> tags, but the content in the <abc></abc> tags will also be displayed on the page. Therefore, the template tag is used here to define the template properties that need to be set in the Vue instance.

<abc id="first">
 <div v-if="flag">{{msg}}<div>
 <div v-else>111<div>
</abc>

The above example can also be written as follows

<!--This page displays hello-->
<div id="app"></div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
 let vm = new Vue({
 el:"#app",
 data:{
  msg:"hello",
  flag:true
 },
 template:"<div v-if='flag'>{{msg}}</div><div v-else>123</div>"//There can only be one root element in a template. If there are multiple, you need to use v-if, v-else, v-else-if to choose which one to display});
</script>

This is the end of this article on the detailed usage of template tags (including a summary of usage in vue). For more relevant content on the usage of template tags, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Examples of three ways to write templates in Vue
  • Summary of four ways to write Vue2 templates
  • How does Vue render the tag content in the template?
  • Detailed explanation of how Vue template supports multiple root nodes
  • Three ways to write template in VUE

<<:  js dynamically generates tables (node ​​operations)

>>:  A brief introduction to Vue filters, lifecycle functions and vue-resource

Recommend

Detailed explanation of MySQL transaction processing usage and example code

MySQL transaction support is not bound to the MyS...

Mysql splits string into array through stored procedure

To split a string into an array, you need to use ...

Use of Linux ifconfig command

1. Command Introduction The ifconfig (configure a...

MySQL trigger definition and usage simple example

This article describes the definition and usage o...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

Introduction to MySQL method of deleting table data with foreign key constraints

When deleting a table or a piece of data in MySQL...

Introduction to basic concepts and technologies used in Web development

Today, this article introduces some basic concept...

How to use Nexus to add jar packages to private servers

Why do we need to build a nexus private server? T...

MySQL batch adding and storing method examples

When logging in to the stress test, many differen...

Ubuntu 18.04 obtains root permissions and logs in as root user

Written in advance: In the following steps, you n...

How to run MySQL using docker-compose

Directory Structure . │ .env │ docker-compose.yml...