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
Here are the detailed steps: 1. Check the disk sp...
1. Create a new configuration file docker_nginx.c...
Copy the following code to the code area of Drea...
Front-end test page code: <template> <di...
Often when we open foreign websites, garbled char...
This article shares the specific code of JS objec...
Overview: The filesystem module is a simple wrapp...
A website uses a lot of HTML5 and CSS3, hoping th...
Code example: public class JDBCDemo3 { public sta...
Through the brief introduction in the previous tw...
Install MySQL for the first time on your machine....
Table of contents Intro Nginx Dockerfile New conf...
1. The window size opened by the HTML hyperlink C...
Create a container [root@server1 ~]# docker run -...
This technique comes from this article - How to a...