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

Mysql classic high-level/command line operation (quick) (recommended)

Since I need to learn how to build servers and da...

Set IE8 to use IE7 style code

<meta http-equiv="x-ua-compatible" co...

Three solutions for sub-functions accessing external variables in JavaScript

Preface When we write web pages, we will definite...

3 ways to add links to HTML select tags

The first one : Copy code The code is as follows: ...

CentOS uses local yum source to build LAMP environment graphic tutorial

This article describes how to use the local yum s...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

Web page HTML code explanation: ordered list and unordered list

In this section, we will learn about list element...

Centos7.3 automatically starts or executes specified commands when booting

In centos7, the permissions of the /etc/rc.d/rc.l...

How to compile the Linux kernel

1. Download the required kernel version 2. Upload...

Detailed explanation of using MySQL where

Table of contents 1. Introduction 2. Main text 2....

A brief discussion on the understanding of TypeScript index signatures

Table of contents 1. What is an index signature? ...