What is the<template>
Element?
was finalized in 2013 to provide a more unified and powerful way to store template text. The specific performance is
-
Get the instantiated HTML element (not just a string) via the
element attribute content
<template id="tpl"> <div>a</div> <div>b</div> </template> <script> const tpl = document.getElementById('tpl') tpl.content // document-fragment tpl.content.children[0].outerHTML // <div>a</div> </script> Copy the code
-
and its children are not visible
-
The SRC attribute of the img element under
does not make a resource request even if it has a value
-
Script and CSS rules under
are not parsed and executed
For more information see: Semantic HTML: The NEW HTML5 Tag — Template
v-if
collocation<template>
<div v-scope="App"></div>
<script type="module"></script>
Copy the code
The first rendering process is as follows:
-
App.$template will be rendered into the DOM tree via the resolveTemplate method in walk.ts
<div v-scope="App"> <template v-if="status === 'offline'"> <span> OFFLINE </span> </template> <template v-else> <span> ONLINE </span> </template> </div> Copy the code
-
- Enter thedirectives/if.tsIdentify the attached
v-if
,v-else
Element and remove them from the DOM tree - According to the conditional expression
status === 'offline'
Used for Dettached nodes<template v-else></template>
Create a Block object based on it
<div v-scope="App"> </div> Copy the code
- Enter thedirectives/if.tsIdentify the attached
-
The
element is recognized in the constructor of the block object, and the child nodes of
are copied as templates by the content.clonenode method for subsequent parsing
<div v-scope="App"> </div> Copy the code
-
Finally, the block object is inserted into the parent and precedes the anchor element in the directives/if.ts
<div v-scope="App"> <span> ONLINE </span> </div> Copy the code
summary
- So we’re using theta
<template>
The element itself features online parsing users invisible (you can’t see me, you can’t see me :D), and avoiding as<img src="logo.png">
Problems with sending invalid requests; - Due to the
<template>
Is in theblock.tsProcessing to get the template, thereforev-for
collocation<template>
The principle andv-if
It’s consistent.
Incorrect use
While
can help us optimize user experience and performance, it can sometimes lead us into holes. Let’s get around these holes!
<div v-scope="App"></div>
<script type="module">
import { createApp } from 'https://unpkg.com/petite-vue?module'
createApp({
App: {
$template: `
Hello
`,}status: 'online'
}).mount('[v-scope]')
</script>
Copy the code
The root block object corresponds to
, and
does not create a new block object for it because there is no v-if or V-for attached. The resulting UI looks like this:
<div v-scope="App">
<template>
<div>Hello</div>
</template>
</div>
Copy the code
Used when the text Hello cannot be seen.
conclusion
must be used with v-if or v-for. We’ll explore the use of @vue/reactivity in Petite-Vue later. Respect the original, reproduced please indicate from: www.cnblogs.com/fsjohnhuang… Fat boy John