文档 / 组件框架

指令集

v-text

<span v-text="msg"></span>
<!-- same as -->
<span>{{msg}}</span>

v-html

<div v-html="html"></div>
<!-- 相同 -->
<div>{{{html}}}</div>

v-if

v-show

v-else

<div v-if="Math.random() > 0.5">
Sorry
</div>
<div v-else>
Not sorry
</div>

v-for

<div v-for="item in items">
{{ item.text }}
</div>

另外也可以为数组索引指定别名(如果值是对象可以为键指定别名):

<div v-for="(index, item) in items"></div>
<div v-for="(key, val) in object"></div>

v-on

<!-- 方法处理器 -->
<button v-on:click="doThis"></button>
<!-- 内联语句 -->
<button v-on:click="doThat('hello', $event)"></button>
<!-- 缩写 -->
<button @click="doThis"></button>
<!-- 停止冒泡 -->
<button @click.stop="doThis"></button>
<!-- 阻止默认行为 -->
<button @click.prevent="doThis"></button>
<!-- 阻止默认行为,没有表达式 -->
<form @submit.prevent></form>
<!-- 串联修饰符 -->
<button @click.stop.prevent="doThis"></button>
<!-- 键修饰符,键别名 -->
<input @keyup.enter="onEnter">
<!-- 键修饰符,键代码 -->
<input @keyup.13="onEnter">

v-bind

<!-- 绑定 attribute -->
<img v-bind:src="imageSrc">
<!-- 缩写 -->
<img :src="imageSrc">
<!-- 绑定 class -->
<div :class="{ red: isRed }"></div>
<div :class="[classA, classB]"></div>
<div :class="[classA, { classB: isB, classC: isC }]"></div>
<!-- 绑定 style -->
<div :style="{ fontSize: size + 'px' }"></div>
<div :style="[styleObjectA, styleObjectB]"></div>
<!-- 绑定到一个对象 -->
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>

v-model

v-el

<span v-el:msg>hello</span>
<span v-el:other-msg>world</span>
this.$els.msg.textContent // -> "hello"
this.$els.otherMsg.textContent // -> "world"

v-pre

<span v-pre>{{ this will not be compiled }}</span>