Using curly braces: {{ greeting }}
Vue3 V Text
# Vue.js 3 v-text Directive
The `v-text` directive in Vue 3 is used to update an element's text content. It works by setting the DOM element's `textContent` property behind the scenes.
---
## Overview
The `v-text` directive sets the text content of an element to the value of the bound expression. It serves as an alternative syntax to the standard mustache interpolation (`{{ }}`) commonly used in Vue templates.
* **Expected Value Type**: `string`
* **Primary Function**: Updates the element's `textContent` property, replacing any existing content.
---
## Basic Usage
Using `v-text` is equivalent to using mustache interpolation (`{{ }}`) inside an empty element.
```html
{{ message }}
```
### Complete Example
Here is a simple Vue component demonstrating both `v-text` and mustache interpolation:
```html
```
---
## Differences Between v-text and Mustache Interpolation
While both achieve similar results, there are key differences in how they behave:
* **Scope**: Mustache interpolation (`{{ }}`) can be placed anywhere within text nodes or combined with other static text. `v-text` must be applied as an attribute on an HTML element.
* **Content Overwriting**: `v-text` will completely overwrite any existing child nodes or text inside the target element. Mustache interpolation allows you to mix dynamic variables with static text inside the same element.
```html
This text will be overwritten
Prefix {{ message }} Suffix
```
---
## Common Use Cases
### 1. Simple Text Binding
When you need to display a single dynamic value inside an element without mixing it with other text, `v-text` provides a clean and explicit syntax:
```html
```
### 2. Performance Optimization with v-once
You can combine `v-text` with the `v-once` directive to render the text only once. Subsequent data updates will not trigger a re-render for that element, which can optimize performance for static content:
```html
```
---
## Important Considerations
> **Overwriting Behavior**: Because `v-text` sets the `textContent` of the element, it will discard any existing HTML tags or text nested inside that element. If you need to preserve surrounding layout or text, use mustache interpolation (`{{ }}`) instead.
> **HTML Escaping**: `v-text` treats all input as plain text and automatically escapes HTML tags for security (preventing XSS attacks). If you need to parse and render actual HTML, use the `v-html` directive instead.
```html
```
YouTip