Skip to content

Commit 8c7cabe

Browse files
committed
v-bind .prop & .attr modifiers
1 parent a747c66 commit 8c7cabe

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

src/api/directives.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@
237237

238238
## v-bind
239239

240-
- **Shorthand:** `:`
240+
- **Shorthand:** `:` or `.` (when using `.prop` modifier)
241241

242242
- **Expects:** `any (with argument) | Object (without argument)`
243243

@@ -246,6 +246,8 @@
246246
- **Modifiers:**
247247

248248
- `.camel` - transform the kebab-case attribute name into camelCase.
249+
- `.prop` - force a binding to be set as DOM property. <Badge text="3.2+"/>
250+
- `.attr` - force a binding to be set as DOM attribute. <Badge text="3.2+"/>
249251

250252
- **Usage:**
251253

@@ -278,23 +280,34 @@
278280
<!-- class binding -->
279281
<div :class="{ red: isRed }"></div>
280282
<div :class="[classA, classB]"></div>
281-
<div :class="[classA, { classB: isB, classC: isC }]">
282-
<!-- style binding -->
283-
<div :style="{ fontSize: size + 'px' }"></div>
284-
<div :style="[styleObjectA, styleObjectB]"></div>
283+
<div :class="[classA, { classB: isB, classC: isC }]"></div>
285284

286-
<!-- binding an object of attributes -->
287-
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
285+
<!-- style binding -->
286+
<div :style="{ fontSize: size + 'px' }"></div>
287+
<div :style="[styleObjectA, styleObjectB]"></div>
288288

289-
<!-- prop binding. "prop" must be declared in my-component. -->
290-
<my-component :prop="someThing"></my-component>
289+
<!-- binding an object of attributes -->
290+
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
291291

292-
<!-- pass down parent props in common with a child component -->
293-
<child-component v-bind="$props"></child-component>
292+
<!-- prop binding. "prop" must be declared in my-component. -->
293+
<my-component :prop="someThing"></my-component>
294294

295-
<!-- XLink -->
296-
<svg><a :xlink:special="foo"></a></svg>
297-
</div>
295+
<!-- pass down parent props in common with a child component -->
296+
<child-component v-bind="$props"></child-component>
297+
298+
<!-- XLink -->
299+
<svg><a :xlink:special="foo"></a></svg>
300+
```
301+
302+
When setting a binding on an element, Vue by default checks whether the element has the key defined as a property using an `in` operator check. If the property is defined, Vue will set the value as DOM property instead of an attribute. This should work in most cases, but you can override this behavior by explicitly using `.prop` or `.attr` modifiers. This is sometimes necessary especially when [working with custom elements](/guide/web-components.html#passing-dom-properties).
303+
304+
The `.prop` modifier also has a dedicated shorthand, `.`:
305+
306+
```html
307+
<div :someProperty.prop="someObject"></div>
308+
309+
<!-- equivalent to -->
310+
<div .someProperty="someObject"></div>
298311
```
299312

300313
The `.camel` modifier allows camelizing a `v-bind` attribute name when using in-DOM templates, e.g. the SVG `viewBox` attribute:
@@ -475,11 +488,7 @@
475488
This directive is provided solely for micro optimizations in performance-critical scenarios and should be rarely needed. The most common case where this may prove helpful is when rendering large `v-for` lists (where `length > 1000`):
476489

477490
```html
478-
<div
479-
v-for="item in list"
480-
:key="itme.id"
481-
v-memo="[item.id === selected]"
482-
>
491+
<div v-for="item in list" :key="itme.id" v-memo="[item.id === selected]">
483492
<p>ID: {{ id }} - selected: {{ item.id === selected }}</p>
484493
<p>...more child nodes</p>
485494
</div>

0 commit comments

Comments
 (0)