指令 - Vue

官网介绍: 内置指令 | Vue.js

<span v-html="rawHtml"></span>

以 HTML attribute 的形式,由 v- 作为前缀

v-bind

绑定 属性(attribute) 、 组件的 prop(property)

缩写: :

.prop 缩写: .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!-- 绑定 attribute -->
<img v-bind:src="va" />

<!-- 缩写形式 -->
<img :src="imageSrc" />

<!-- 动态 attribute 名 -->
<button v-bind:[key]="value"></button>

<!-- 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>

<!-- 绑定对象形式的 attribute -->
<div v-bind="{ id: someProp, 'other-attr': otherProp }"></div>

<!-- prop 绑定。“prop” 必须在子组件中已声明。 -->
<MyComponent :prop="someThing" />

<!-- 传递子父组件共有的 prop -->
<MyComponent v-bind="$props" />

<!-- XLink -->
<svg><a :xlink:special="foo"></a></svg>

v-html

<span v-html="rawHtml"></span>

将此元素的 innerHTML 与 rawHtml 属性保持同步。

v-once

仅渲染元素和组件一次,并跳过之后的更新。(后续更改不再继续更新)

这可以用来优化更新时的性能。

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 单个元素 -->
<span v-once>This will never change: {{msg}}</span>
<!-- 带有子元素的元素 -->
<div v-once>
<h1>comment</h1>
<p>{{msg}}</p>
</div>
<!-- 组件 -->
<MyComponent v-once :comment="msg" />
<!-- `v-for` 指令 -->
<ul>
<li v-for="i in list" v-once>{{i}}</li>
</ul>

v-for

可选的第二个参数表示当前项的位置索引。

1
2
3
4
5
6
data() {
return {
parentMessage: 'Parent',
items: [{ message: 'Foo' }, { message: 'Bar' }]
}
}
1
2
3
4
5
<!-- template -->

<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>