Vue 3 v-text Directive
v-text The directive is used to update an element's text content, equivalent to setting the DOM element's textContent attribute.
Basic Explanation
v-text The directive sets the value of its bound expression as the element's text content. It is the most commonly used text interpolation {{ }} 's alternative syntax.
- Expected type: string
- Purpose: updates the element's text content, equivalent to setting the textContent property.
Basic Usage
Example
{{ message }}
Example
Using braces: {{ greeting }}
Difference Between v-text and Braces
v-text and {{ }} The main difference from brace interpolation is:
{{ }}can be used in both text nodes and attributesv-textcan only be used for an element's text contentv-textoverwrites the element's original text content
Example
the original text is overwritten
prefix {{ message }} suffix
Use Cases
1. Simple Text Binding
When you only need to display text without mixing in other content, v-text is a clear choice:
Example
2. Used together with v-once
v-text can be used together with v-once, rendering only once and not updating afterward:
Example
Update text
Notes
Overwrite existing content: v-text completely overwrites all text content inside the element. If you only need to update part of the text, you should use mustache interpolation.
{{ }}.
HTML escaping: v-text treats content as plain text and does not parse HTML tags. If you need to render HTML, you should use the v-html directive.
Example
Vue 3 Built-in Directives