1. Template tag in HTML5 The content in the <!--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
<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 Vue1. The template tag is inside the element bound to the vue instance
<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
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 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:
|
<<: js dynamically generates tables (node operations)
>>: A brief introduction to Vue filters, lifecycle functions and vue-resource
Table of contents 1. New II. Modification element...
Since I need to learn how to build servers and da...
<meta http-equiv="x-ua-compatible" co...
Preface When we write web pages, we will definite...
A brief introduction to protobuf Protobuf is Goog...
The first one : Copy code The code is as follows: ...
Uses of new The function of new is to create an i...
Set the width of the body to the width of the wind...
This article describes how to use the local yum s...
student.xml <?xml version="1.0" enco...
In this section, we will learn about list element...
In centos7, the permissions of the /etc/rc.d/rc.l...
1. Download the required kernel version 2. Upload...
Table of contents 1. Introduction 2. Main text 2....
Table of contents 1. What is an index signature? ...