Skip to content

Commit 42bb5df

Browse files
committed
add directives to API docs
1 parent 836374a commit 42bb5df

File tree

1 file changed

+322
-0
lines changed

1 file changed

+322
-0
lines changed

src/api/index.md

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,325 @@ type: api
820820
Triggers the `beforeDestroy` and `destroyed` hooks.
821821

822822
- **See also:** [Lifecycle Diagram](/guide/instance.html#Lifecycle-Diagram)
823+
824+
## Directives
825+
826+
### v-text
827+
828+
- **Expects:** `String`
829+
830+
- **Details:**
831+
832+
Updates the element's `textContent`. If you need to update the part of `textContent`, you should use `{% raw %}{{ Mustache }}{% endraw %}` interpolations.
833+
834+
- **Example:**
835+
836+
```html
837+
<span v-text="msg"></span>
838+
<!-- same as -->
839+
<span>{{msg}}</span>
840+
```
841+
842+
- **See also:** [Data Binding Syntax - interpolations](/guide/syntax.html#Text)
843+
844+
### v-html
845+
846+
- **Expects:** `String`
847+
848+
- **Details:**
849+
850+
Updates the element's `innerHTML`. The contents are inserted as plain HTML - data bindings are ignored. If you need to reuse template pieces, you should use [functional components](!!TODO).
851+
852+
<p class="tip">Dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting). Only use `v-html` on trusted content and **never** on user-provided content.</p>
853+
854+
- **Example:**
855+
856+
```html
857+
<div v-html="html"></div>
858+
```
859+
- **See also:** [Data Binding Syntax - interpolations](/guide/syntax.html#Raw-HTML)
860+
861+
### v-if
862+
863+
- **Expects:** `*`
864+
865+
- **Usage:**
866+
867+
Conditionally render the element based on the truthy-ness of the expression value. The element and its contained data bindings / components are destroyed and re-constructed during toggles. If the element is a `<template>` element, its content will be extracted as the conditional block.
868+
869+
- **See also:** [Conditional Rendering - v-if](/guide/conditional.html)
870+
871+
### v-show
872+
873+
- **Expects:** `*`
874+
875+
- **Usage:**
876+
877+
Toggle's the element's `display` CSS property based on the truthy-ness of the expression value. Triggers transitions if present.
878+
879+
- **See also:** [Conditional Rendering - v-show](/guide/conditional.html#v-show)
880+
881+
### v-else
882+
883+
- **Does not expect expression**
884+
885+
- **Restriction:** previous sibling element must have `v-if` or `v-show`.
886+
887+
- **Usage:**
888+
889+
Denote the "else block" for `v-if` and `v-show`.
890+
891+
```html
892+
<div v-if="Math.random() > 0.5">
893+
Sorry
894+
</div>
895+
<div v-else>
896+
Not sorry
897+
</div>
898+
```
899+
900+
- **See also:**
901+
- [Conditional Rendering - v-else](/guide/conditional.html#v-else)
902+
- [Conditional Rendering - Component caveat](/guide/conditional.html#Component-caveat)
903+
904+
### v-for
905+
906+
- **Expects:** `Array | Object | Number | String`
907+
908+
- **Param Attributes:**
909+
- [`key`](/guide/list.html#key)
910+
911+
- **Usage:**
912+
913+
Render the element or template block multiple times based on the source data. The directive's value must use the special syntax `alias in expression` to provide an alias for the current element being iterated on:
914+
915+
```html
916+
<div v-for="item in items">
917+
{{ item.text }}
918+
</div>
919+
```
920+
921+
Alternatively, you can also specify an alias for the index (or the key if used on an Object):
922+
923+
```html
924+
<div v-for="(item, index) in items"></div>
925+
<div v-for="(key, val) in object"></div>
926+
<div v-for="(key, val, index) in object"></div>
927+
```
928+
929+
The detailed usage for `v-for` is explained in the guide section linked below.
930+
931+
- **See also:** [List Rendering](/guide/list.html).
932+
933+
### v-on
934+
935+
- **Shorthand:** `@`
936+
937+
- **Expects:** `Function | Inline Statement`
938+
939+
- **Argument:** `event (required)`
940+
941+
- **Modifiers:**
942+
- `.stop` - call `event.stopPropagation()`.
943+
- `.prevent` - call `event.preventDefault()`.
944+
- `.capture` - add event listener in capture mode.
945+
- `.self` - only trigger handler if event was dispatched from this element.
946+
- `.{keyCode | keyAlias}` - only trigger handler on certain keys.
947+
- `.native` - listen for a native event on the root element of component.
948+
949+
- **Usage:**
950+
951+
Attaches an event listener to the element. The event type is denoted by the argument. The expression can either be a method name or an inline statement, or simply omitted when there are modifiers present.
952+
953+
When used on a normal element, it listens to **native DOM events** only. When used on a custom element component, it also listens to **custom events** emitted on that child component.
954+
955+
When listening to native DOM events, the method receives the native event as the only argument. If using inline statement, the statement has access to the special `$event` property: `v-on:click="handle('ok', $event)"`.
956+
957+
- **Example:**
958+
959+
```html
960+
<!-- method handler -->
961+
<button v-on:click="doThis"></button>
962+
963+
<!-- inline statement -->
964+
<button v-on:click="doThat('hello', $event)"></button>
965+
966+
<!-- shorthand -->
967+
<button @click="doThis"></button>
968+
969+
<!-- stop propagation -->
970+
<button @click.stop="doThis"></button>
971+
972+
<!-- prevent default -->
973+
<button @click.prevent="doThis"></button>
974+
975+
<!-- prevent default without expression -->
976+
<form @submit.prevent></form>
977+
978+
<!-- chain modifiers -->
979+
<button @click.stop.prevent="doThis"></button>
980+
981+
<!-- key modifier using keyAlias -->
982+
<input @keyup.enter="onEnter">
983+
984+
<!-- key modifier using keyCode -->
985+
<input @keyup.13="onEnter">
986+
```
987+
988+
Listening to custom events on a child component (the handler is called when "my-event" is emitted on the child):
989+
990+
```html
991+
<my-component @my-event="handleThis"></my-component>
992+
993+
<!-- inline statement -->
994+
<my-component @my-event="handleThis(123, $event)"></my-component>
995+
```
996+
997+
- **See also:**
998+
- [Methods and Event Handling](/guide/events.html)
999+
- [Components - Custom Events](/guide/components.html#Custom-Events)
1000+
1001+
### v-bind
1002+
1003+
- **Shorthand:** `:`
1004+
1005+
- **Expects:** `* (with argument) | Object (without argument)`
1006+
1007+
- **Argument:** `attrOrProp (optional)`
1008+
1009+
- **Modifiers:**
1010+
- `.prop` - Used for binding DOM attributes.
1011+
1012+
- **Usage:**
1013+
1014+
Dynamically bind one or more attributes, or a component prop to an expression.
1015+
1016+
When used to bind the `class` or `style` attribute, it supports additional value types such as Array or Objects. See linked guide section below for more details.
1017+
1018+
When used for prop binding, the prop must be properly declared in the child component.
1019+
1020+
When used without an argument, can be used to bind an object containing attribute name-value pairs. Note in this mode `class` and `style` does not support Array or Objects.
1021+
1022+
- **Example:**
1023+
1024+
```html
1025+
<!-- bind an attribute -->
1026+
<img v-bind:src="imageSrc">
1027+
1028+
<!-- shorthand -->
1029+
<img :src="imageSrc">
1030+
1031+
<!-- class binding -->
1032+
<div :class="{ red: isRed }"></div>
1033+
<div :class="[classA, classB]"></div>
1034+
<div :class="[classA, { classB: isB, classC: isC }]">
1035+
1036+
<!-- style binding -->
1037+
<div :style="{ fontSize: size + 'px' }"></div>
1038+
<div :style="[styleObjectA, styleObjectB]"></div>
1039+
1040+
<!-- binding an object of attributes -->
1041+
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>
1042+
1043+
<!-- DOM attribute binding with prop modifier -->
1044+
<div v-bind:text-content.prop="text"></div>
1045+
1046+
<!-- prop binding. "prop" must be declared in my-component. -->
1047+
<my-component :prop="someThing"></my-component>
1048+
1049+
<!-- XLink -->
1050+
<svg><a :xlink:special="foo"></a></svg>
1051+
```
1052+
1053+
- **See also:**
1054+
- [Class and Style Bindings](/guide/class-and-style.html)
1055+
- [Components - Component Props](/guide/components.html#Props)
1056+
1057+
### v-model
1058+
1059+
- **Expects:** varies based on value of form inputs element or output of components
1060+
1061+
- **Limited to:**
1062+
- `<input>`
1063+
- `<select>`
1064+
- `<textarea>`
1065+
- components
1066+
1067+
- **Param Attributes:**
1068+
- [`lazy`](/guide/forms.html#lazy)
1069+
- [`number`](/guide/forms.html#number)
1070+
- [`trim`](/guild/forms.html#trim)
1071+
1072+
- **Usage:**
1073+
1074+
Create a two-way binding on a form input element or a component. For detailed usage, see guide section linked below.
1075+
1076+
- **See also:**
1077+
- [Form Input Bindings](/guide/forms.html)
1078+
- [Components - Parent-Child Commnunication](/guide/components.html#Form-Input-Components-using-Custom-Events)
1079+
1080+
### v-pre
1081+
1082+
- **Does not expect expression**
1083+
1084+
- **Usage**
1085+
1086+
Skip compilation for this element and all its children. You can use this for displaying raw mustache tags. Skipping large numbers of nodes with no directives on them can also speed up compilation.
1087+
1088+
- **Example:**
1089+
1090+
```html
1091+
<span v-pre>{{ this will not be compiled }}</span>
1092+
```
1093+
1094+
### v-cloak
1095+
1096+
- **Does not expect expression**
1097+
1098+
- **Usage:**
1099+
1100+
This directive will remain on the element until the associated Vue instance finishes compilation. Combined with CSS rules such as `[v-cloak] { display: none }`, this directive can be used to hide un-compiled mustache bindings until the Vue instance is ready.
1101+
1102+
- **Example:**
1103+
1104+
```css
1105+
[v-cloak] {
1106+
display: none;
1107+
}
1108+
```
1109+
1110+
```html
1111+
<div v-cloak>
1112+
{{ message }}
1113+
</div>
1114+
```
1115+
1116+
The `<div>` will not be visible until the compilation is done.
1117+
1118+
### v-once
1119+
1120+
- **Does not expect expression**
1121+
1122+
- **Details:**
1123+
1124+
Evaluates the element and component **once** only.
1125+
1126+
```html
1127+
<!-- single element -->
1128+
<span v-once>This will never change: {{msg}}</span>
1129+
<!-- the element have children -->
1130+
<div v-once>
1131+
<h1>comment</h1>
1132+
<p>{{msg}}</p>
1133+
</div>
1134+
<!-- component -->
1135+
<my-component v-once :comment="msg"></my-component>
1136+
<!-- v-for directive -->
1137+
<ul>
1138+
<li v-for="i in list" v-once>{{i}}</li>
1139+
</ul>
1140+
```
1141+
1142+
- **See also:**
1143+
- [Data Binding Syntax - interpolations](/guide/syntax.html#Text)
1144+
- [Components - Cheap Static Components with v-once](/guide/components.html#Cheap-Static-Components-with-v-once)

0 commit comments

Comments
 (0)