You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/template-syntax.md
+24-26Lines changed: 24 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -12,26 +12,27 @@ If you are familiar with Virtual DOM concepts and prefer the raw power of JavaSc
12
12
13
13
The most basic form of data binding is text interpolation using the "Mustache" syntax (double curly braces):
14
14
15
-
```html
15
+
```html
16
16
<span>Message: {{ msg }}</span>
17
17
```
18
18
19
19
The mustache tag will be replaced with the value of the `msg` property on the corresponding data object. It will also be updated whenever the data object's `msg` property changes.
20
20
21
21
You can also perform one-time interpolations that do not update on data change by using the [v-once directive](TODO:../api/#v-once), but keep in mind this will also affect any other bindings on the same node:
22
22
23
-
```html
23
+
```html
24
24
<spanv-once>This will never change: {{ msg }}</span>
25
25
```
26
26
27
27
### Raw HTML
28
28
29
29
The double mustaches interprets the data as plain text, not HTML. In order to output real HTML, you will need to use the [`v-html` directive](TODO:../api/#v-html):
The contents of the `span` will be replaced with the value of the `rawHtml` property, interpreted as plain HTML - data bindings are ignored. Note that you cannot use `v-html` to compose template partials, because Vue is not a string-based templating engine. Instead, components are preferred as the fundamental unit for UI reuse and composition.
@@ -44,13 +45,13 @@ Dynamically rendering arbitrary HTML on your website can be very dangerous becau
44
45
45
46
Mustaches cannot be used inside HTML attributes. Instead, use a [`v-bind` directive](../api/#v-bind):
46
47
47
-
```html
48
+
```html
48
49
<divv-bind:id="dynamicId"></div>
49
50
```
50
51
51
52
In the case of boolean attributes, where their mere existence implies `true`, `v-bind` works a little differently. In this example:
@@ -60,19 +61,16 @@ If `isButtonDisabled` has the value of `null`, `undefined`, or `false`, the `dis
60
61
61
62
So far we've only been binding to simple property keys in our templates. But Vue.js actually supports the full power of JavaScript expressions inside all data bindings:
62
63
63
-
```html
64
-
{{ number + 1 }}
65
-
66
-
{{ ok ? 'YES' : 'NO' }}
67
-
68
-
{{ message.split('').reverse().join('') }}
64
+
```html
65
+
{{ number + 1 }} {{ ok ? 'YES' : 'NO' }} {{ message.split('').reverse().join('')
66
+
}}
69
67
70
68
<divv-bind:id="'list-' + id"></div>
71
69
```
72
70
73
71
These expressions will be evaluated as JavaScript in the data scope of the owner Vue instance. One restriction is that each binding can only contain **one single expression**, so the following will **NOT** work:
74
72
75
-
```html
73
+
```html
76
74
<!-- this is a statement, not an expression: -->
77
75
{{ var a = 1 }}
78
76
@@ -88,7 +86,7 @@ Template expressions are sandboxed and only have access to a [whitelist of globa
88
86
89
87
Directives are special attributes with the `v-` prefix. Directive attribute values are expected to be **a single JavaScript expression** (with the exception of `v-for`, which will be discussed later). A directive's job is to reactively apply side effects to the DOM when the value of its expression changes. Let's review the example we saw in the introduction:
90
88
91
-
```html
89
+
```html
92
90
<pv-if="seen">Now you see me</p>
93
91
```
94
92
@@ -98,15 +96,15 @@ Here, the `v-if` directive would remove/insert the `<p>` element based on the tr
98
96
99
97
Some directives can take an "argument", denoted by a colon after the directive name. For example, the `v-bind` directive is used to reactively update an HTML attribute:
100
98
101
-
```html
99
+
```html
102
100
<av-bind:href="url"> ... </a>
103
101
```
104
102
105
103
Here `href` is the argument, which tells the `v-bind` directive to bind the element's `href` attribute to the value of the expression `url`.
106
104
107
105
Another example is the `v-on` directive, which listens to DOM events:
108
106
109
-
```html
107
+
```html
110
108
<av-on:click="doSomething"> ... </a>
111
109
```
112
110
@@ -116,7 +114,7 @@ Here the argument is the event name to listen to. We will talk about event handl
116
114
117
115
It is also possible to use a JavaScript expression in a directive argument by wrapping it with square brackets:
118
116
119
-
```html
117
+
```html
120
118
<!--
121
119
Note that there are some constraints to the argument expression, as explained
122
120
in the "Dynamic Argument Expression Constraints" section below.
@@ -128,7 +126,7 @@ Here `attributeName` will be dynamically evaluated as a JavaScript expression, a
128
126
129
127
Similarly, you can use dynamic arguments to bind a handler to a dynamic event name:
130
128
131
-
```html
129
+
```html
132
130
<av-on:[eventName]="doSomething"> ... </a>
133
131
```
134
132
@@ -142,7 +140,7 @@ Dynamic arguments are expected to evaluate to a string, with the exception of `n
142
140
143
141
Dynamic argument expressions have some syntax constraints because certain characters, such as spaces and quotes, are invalid inside HTML attribute names. For example, the following is invalid:
144
142
145
-
```html
143
+
```html
146
144
<!-- This will trigger a compiler warning. -->
147
145
<av-bind:['foo'+bar]="value"> ... </a>
148
146
```
@@ -151,7 +149,7 @@ The workaround is to either use expressions without spaces or quotes, or replace
151
149
152
150
When using in-DOM templates (templates directly written in an HTML file), you should also avoid naming keys with uppercase characters, as browsers will coerce attribute names into lowercase:
153
151
154
-
```html
152
+
```html
155
153
<!--
156
154
This will be converted to v-bind:[someattr] in in-DOM templates.
157
155
Unless you have a "someattr" property in your instance, your code won't work.
@@ -163,32 +161,32 @@ Unless you have a "someattr" property in your instance, your code won't work.
163
161
164
162
Modifiers are special postfixes denoted by a dot, which indicate that a directive should be bound in some special way. For example, the `.prevent` modifier tells the `v-on` directive to call `event.preventDefault()` on the triggered event:
165
163
166
-
```html
167
-
<formv-on:submit.prevent="onSubmit">...</form>
164
+
```html
165
+
<formv-on:submit.prevent="onSubmit">...</form>
168
166
```
169
167
170
-
You'll see other examples of modifiers later, [for `v-on`](TODO:events.html#Event-Modifiers) and [for `v-model`](TODO:forms.html#Modifiers), when we explore those features.
168
+
You'll see other examples of modifiers later, [for `v-on`](events.md#event-modifiers) and [for `v-model`](forms.md#modifiers), when we explore those features.
171
169
172
170
## Shorthands
173
171
174
172
The `v-` prefix serves as a visual cue for identifying Vue-specific attributes in your templates. This is useful when you are using Vue.js to apply dynamic behavior to some existing markup, but can feel verbose for some frequently used directives. At the same time, the need for the `v-` prefix becomes less important when you are building a [SPA](https://en.wikipedia.org/wiki/Single-page_application), where Vue manages every template. Therefore, Vue provides special shorthands for two of the most often used directives, `v-bind` and `v-on`:
175
173
176
174
### `v-bind` Shorthand
177
175
178
-
```html
176
+
```html
179
177
<!-- full syntax -->
180
178
<av-bind:href="url"> ... </a>
181
179
182
180
<!-- shorthand -->
183
181
<a:href="url"> ... </a>
184
182
185
-
<!-- shorthand with dynamic argument (2.6.0+) -->
183
+
<!-- shorthand with dynamic argument -->
186
184
<a:[key]="url"> ... </a>
187
185
```
188
186
189
187
### `v-on` Shorthand
190
188
191
-
```html
189
+
```html
192
190
<!-- full syntax -->
193
191
<av-on:click="doSomething"> ... </a>
194
192
@@ -199,4 +197,4 @@ The `v-` prefix serves as a visual cue for identifying Vue-specific attributes i
199
197
<a@[event]="doSomething"> ... </a>
200
198
```
201
199
202
-
They may look a bit different from normal HTML, but `:` and `@` are valid characters for attribute names and all Vue-supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is totally optional, but you will likely appreciate it when you learn more about its usage later.
200
+
They may look a bit different from normal HTML, but `:` and `@` are valid characters for attribute names and all Vue-supported browsers can parse it correctly. In addition, they do not appear in the final rendered markup. The shorthand syntax is totally optional, but you will likely appreciate it when you learn more about its usage later.
0 commit comments