Home > front end >  The difference between string template and DOM template
The difference between string template and DOM template

Time:03-17

Recently saw vue official document, repeatedly mentioned string template and dom template, comparison on the concept of fuzzy, check postscript LuGong reference in the future,

1. The string template
String template is written in the vue template defined in the template, such as. Vue of single component template file and define the component template attribute value templates, template string not initialized to participate in the rendering of the page in the page will be vue parsing the compiled by browsers render again, so is not limited to the HTML structure and name tag,

Vue.com ponent (' MyComponentA '{
The template: '& lt; Div MyId="123" & gt; Hello, world
'
})



2. Dom template (or called Html template)
Dom template is written in HTML file, open the browser will be parsed rendering, so we must follow the naming of HTML structure and labels, otherwise the browser does not parse also cannot access to content,

The following example will not be correctly apply colours to a drawing, will be parsed into mycomponent, but registered vue components is mycomponent, unable to render,



<meta charset="utf-8" & gt;
Vue Component

<body>
Hello Vue


<script SRC="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js" & gt; </script>
<script & gt;
//global registration
Vue.com ponent (' MyComponent, {
The template: '& lt; Div> Component classes let & lt;/div> '
});
New Vue ({
El: '# app'
});
</script>


So, this example can be normal shows:



<meta charset="utf-8" & gt;
Vue Component

<body>
Hello Vue


<script SRC="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js" & gt; </script>
<script & gt;
//global registration
Vue.com ponent (' my - component, {
The template: '& lt; Div> Component classes let & lt;/div> '
});
New Vue ({
El: '# app'
});
</script>


Because HTML for case-insensitive, so using the component must be used in DOM template kebab - case nomenclature (dash),
Therefore, for component name naming, refer to the following:


/* - components in a single file, JSX and string in the template - */
/* - in DOM template - */

Or
/* - in all places - */
  • Related