Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Template Syntax

Vue 3 Template Syntax

Vue uses an HTML-based template syntax that lets developers declaratively bind the DOM to the underlying Vue instance's data.

Vue's core is a system that lets you declaratively render data into the DOM using a concise template syntax.

Combined with the reactivity system, when the application state changes, Vue intelligently computes the minimal cost to re-render components and applies it to DOM operations.

Example

<

template

<

div

<

p

{

{

message

}

}

p

<

a

:

href

=

"url"

Link

a

   

   

   

<

button

@

click

=

"handleClick"

Click me

button

   

   

   

<

ul

<

li v

for

=

"item in items"

:

key

=

"item.id"

{

{

item.

name

}

}

li

   

ul

 

div

template


Interpolation

Text

The most common form of data binding is text interpolation using {{...}} (double curly braces):

Text Interpolation

<div id="app">
<p>

{{ message }}

p

div

Try it »

The content of the {{...}} tag will be replaced with message 's property value. If message 's property value changes, the {{...}} tag content updates as well.

If you do not want the tag content to change, you can use the v-once directive to perform a one-time interpolation; when the data changes, the interpolated content will not update.

这个将不会改变: {{ message }}

Html

Use the v-html directive to output HTML code:

v-html directive

<div id="example1" class="demo">
<p>

Use double curly brace text interpolation: {{ rawHtml }}

p

<

p

Use the v-html directive:

<span v-html="rawHtml" span p div>
<script>

const RenderHtmlApp = { data() { return { rawHtml: '

<span style=">

color: red

"

This will display in red!

span

' } } } Vue.createApp(RenderHtmlApp).mount('#example1')

script

Try it »

property

Values in HTML attributes should use the v-bind directive.

For boolean attributes, the normal value is true or false; if the attribute value is null or undefined, the attribute is not displayed.



In the code above, if isButtonDisabled is null or undefined, the disabled attribute will not even be included in the rendered