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
MySQL transaction support is not bound to the MyS...
To split a string into an array, you need to use ...
1. Command Introduction The ifconfig (configure a...
This article describes the definition and usage o...
Table of contents 1. Function Binding 2. With par...
MySQL is a database that I like very much. Today,...
Copy the following code to the code area of Drea...
Table of contents Example Code Rendering Code Ana...
When deleting a table or a piece of data in MySQL...
Today, this article introduces some basic concept...
Why do we need to build a nexus private server? T...
After reinstalling the system today, I reinstalle...
When logging in to the stress test, many differen...
Written in advance: In the following steps, you n...
Directory Structure . │ .env │ docker-compose.yml...