Skip to content

Commit 746f128

Browse files
committed
2.6: dynamic arguments
1 parent cdf7ca3 commit 746f128

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/v2/guide/syntax.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ Another example is the `v-on` directive, which listens to DOM events:
128128

129129
Here the argument is the event name to listen to. We will talk about event handling in more detail too.
130130

131+
### Dynamic Arguments
132+
133+
> New in 2.6
134+
135+
Starting in 2.6, it is also possible to use JavaScript expressions in an directive argument by wrapping it with square brackets:
136+
137+
``` html
138+
<a v-bind:[key]="url"> ... </a>
139+
```
140+
141+
Here `key` will be dynamically evaluated as a JavaScript expression, and its evaluated value will be used as the final value for the argument. For example, if your Vue instance has a data property `key` whose value is `"href"`, then this binding will be equivalent to `v-bind:href`.
142+
143+
Similarly, you can use dynamic arguments to bind a handler to a dynamic event:
144+
145+
``` html
146+
<a v-on:[event]="doSomething"> ... </a>
147+
```
148+
149+
#### Dynamic Argument Value Constraints
150+
151+
Dynamic argument values are expected to be strings, with the exception of `null`. The special value `null` can be used to explicitly remove the binding. Any other non-string value will trigger a warning.
152+
153+
#### Dynamic Argument Expression Constraints
154+
155+
When using dynamic arguments, it is important to remember that HTML attribute names cannot contain spaces or quotes. For example, the following is invalid:
156+
157+
``` html
158+
<a v-bind:['foo' + bar]="value"> ... </a>
159+
```
160+
161+
The Vue template compiler will warn against such usage. The workaround is to either use expressions without spaces or quotes, or use a computed property instead.
162+
131163
### Modifiers
132164

133165
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:

0 commit comments

Comments
 (0)