```
### 指令
```html
现在你看到我了
```
`v-if` 指令将根据表达式 `seen` 的值的真假来插入/移除 \
元素
### 指令参数
```html
...
```
`v-bind` 指令将该元素 `href` 属性与表达式 `url` 的值绑定
```html
...
```
`v-on` 指令,用于监听 DOM 事件,oSomething 是事件名
### 指令动态参数 **v2.6**
```html
...
```
当 `eventName` 的值为 `focus` 时,`v-on:[eventName]` 将等价于 `v-on:focus`
### 指令修饰符
```html
```
`.prevent` 修饰符告诉 `v-on` 指令对于触发的事件调用 `event.preventDefault()`
### 指令缩写
```html
...
...
...
```
Class 与 Style 绑定
---
### 对象语法
```html
```
传给 `v-bind:class` 一个对象,以动态地切换 `class`
### 与普通的 class 属性共存
```html {2}
```
如下 data
```js
data: {
isActive: true,
hasError: false
}
```
结果渲染为
```html
```
### 绑定的数据对象不必内联定义在模板里
```html
```
如下 data
```js
data: {
classObject: {
active: true,
'text-danger': false
}
}
```
结果渲染为
```html
```
### 三元表达式
```html
```
### 数组
```html
```
### 数组语法
```html
```
如下 data
```js
data: {
activeClass: 'active',
errorClass: 'text-danger'
}
```
结果渲染为
```html
```
### 内联样式
```html
```
如下 data
```js
data: {
activeColor: 'red',
fontSize: 30
}
```
结果渲染为
```html
```
### 内联样式对象通常更好
```html
```
如下 data
```js
data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}
```
同样的,对象语法常常结合返回对象的计算属性使用
### 内联样式数组语法
```html
```
### 内联样式多重值
```html
```
条件渲染
---
### v-if
```html
Vue is awesome!
Oh no 😢
```
### v-else-if
```html
A
B
C
Not A/B/C
```
`@2.1.0` 新增,必须紧跟在带 `v-if` 或者 `v-else-if` 的元素之后
### v-else
```html
现在你看到我了
现在你看不见我了
```
`v-else` 元素必须紧跟在带 `v-if` 或者 `v-else-if` 的元素的后面
### \
上使用 v-if 条件渲染分组
```html
Paragraph 1
```
### 用 key 管理可复用的元素
```html
Username
Email
```
### v-show
```html
Hello!
```
带有 `v-show` 的元素始终会被渲染并保留在 DOM 中,只是简单地切换元素的 `CSS` 属性 `display`
列表渲染
---
### v-for
```html {3}
```
```js
var example1 = new Vue({
el: '#example-1',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
```
### v-for 可选的第二个参数(索引)
```html {2}
{{ index }}
{{ item.message }}
```
如下 `data`
```js
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
```
也可以用 `of` 替代 `in` 作为分隔符
```html
```
### v-for 使用对象
```html
{{ value }}
```
如下 data
```js
data: {
object: {
title: 'How to do lists in Vue',
author: 'Jane Doe',
publishedAt: '2016-04-10'
}
}
```
渲染结果
```
How to do lists in Vue
Jane Doe
2016-04-10
```
提供第二个的参数为 property 名称 (也就是键名)
```html
{{ name }}: {{ value }}
```
还可以用第三个参数作为索引
```html
{{ index }}. {{ name }}: {{ value }}
```
### v-for/v-if
```html {2,3}
{{ todo }}
```
只渲染未完成的 todo,下面加上 `v-else` 示例
```html
No todos left!
```
### 组件上使用 v-for
```html
```
`2.2.0+` 的版本里,当在组件上使用 `v-for` 时,`key` 现在是必须的
事件处理
---
### 监听事件
```html {2}
+1
按钮已被点击 {{ counter }} 次。
```
```js
var example1 = new Vue({
el: '#example-1',
data: {
counter: 0
}
})
```
### 事件处理方法
```html {3}
你好
```
```js
var example2 = new Vue({
el: '#example-2',
data: {
name: 'Vue.js'
},
// 在 `methods` 对象中定义方法
methods: {
greet: function (event) {
// `this` 在方法里指向当前 Vue 实例
alert('Hello ' + this.name + '!')
// `event` 是原生 DOM 事件
if (event) {
alert(event.target.tagName)
}
}
}
})
```
也可以用 JavaScript 直接调用方法
```js
example2.greet() // => 'Hello Vue.js!'
```
### 内联处理器中的方法
```html {2,5}
弹出 hi
弹出 what
```
```js {4}
new Vue({
el: '#example-3',
methods: {
say: function (message) {
alert(message)
}
}
})
```
访问原始的 DOM 事件,用特殊变量 $event
```html
提交
```
```js
methods: {
say: function (message, event) {
// 现在我们可以访问原生事件对象
if (event) {
event.preventDefault()
}
alert(message)
}
}
```
### 事件修饰符
```html
...
...
```
### 事件修饰符 passive
```html
...
```
这个 `.passive` [修饰符](#v-on-事件修饰符)尤其能够提升移动端的性能。
### 按键修饰符
```html
```
### .exact 修饰符
```html
```
计算属性和侦听器
---
### 基础例子
```html
Original message: "{{ message }}"
计算的反向消息: "{{ reversedMessage }}"
```
```js
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
// 计算属性的 getter
reversedMessage: function () {
// `this` 指向 vm 实例
return this.message.split('')
.reverse().join('')
}
}
})
```
### 计算属性缓存 vs 方法
```html
计算的反向消息:"{{ reversedMessage() }}"
```
在组件中,我们可以将同一函数定义为一个方法而不是一个计算属性
```js
methods: {
reversedMessage: function () {
return this.message.split('')
.reverse().join('')
}
}
```
两种方式的最终结果确实是完全相同的。然而,不同的是**计算属性是基于它们的响应式依赖进行缓存的**。
### 计算属性 vs 侦听属性
```html
{{ fullName }}
```
```js
var vm = new Vue({
el: '#demo',
data: {
firstName: 'Foo',
lastName: 'Bar',
fullName: 'Foo Bar'
},
watch: {
firstName: function (val) {
this.fullName =
val + ' ' + this.lastName
},
lastName: function (val) {
this.fullName =
this.firstName + ' ' + val
}
}
})
```
上面代码是命令式且重复的。将它与计算属性的版本进行比较:
```js
var vm = new Vue({
el: '#demo',
data: {
firstName: 'Foo',
lastName: 'Bar'
},
computed: {
fullName: function () {
return this.firstName
+ ' ' + this.lastName
}
}
})
```
### 计算属性的 setter
```js
computed: {
fullName: {
get: function () { // getter
return this.firstName + ' ' + this.lastName
},
set: function (newValue) { // setter
var names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[names.length - 1]
}
}
}
```
表单输入绑定
---
### 文本
```html
msg is: {{ msg }}
```
### 多行文本
```html {3}
Multiline message is:
{{ message }}
```
### 复选框
```html {4}
{{ checked}}
```
### 多个复选框
```html
Jack
John
Mike
Checked names: {{ checkedNames }}
```
如下 data
```js
new Vue({
el: '...',
data: {
checkedNames: []
}
})
```
### 单选按钮
```html
```
---
```js
new Vue({
el: '#example-4',
data: {
picked: ''
}
})
```
### 选择框
```html
请选择
A
B
C
Selected: {{ selected }}
```
---
```js
new Vue({
el: '...',
data: {
selected: ''
}
})
```
### 选择框(数组)
```html
A
B
C
Selected: {{ selected }}
```
---
```js
new Vue({
el: '...',
data: {
selected: []
}
})
```
### v-for 渲染的动态选项
```html {3-4}
{{ option.text }}
Selected: {{ selected }}
```
---
```js {6-8}
new Vue({
el: '...',
data: {
selected: 'A',
options: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}
})
```
### 值绑定
```html
ABC
```
### 单选按钮
```html
```
当选中时
```js
vm.pick === vm.a
```
### 复选框
```html {3}
```
---
```js
// 当选中时
vm.toggle === 'yes'
// 当没有选中时
vm.toggle === 'no'
```
### 选择框的选项
```html
123
```
当选中时
```js
typeof vm.selected // => 'object'
vm.selected.number // => 123
```
### 修饰符
```html
```
组件基础
---
### 基本示例
```js
Vue.component('button-counter', {
data: function () {
return {
count: 0
}
},
template: `
你点击了我 {{ count }} 次
`
})
```
组件是可复用的 `Vue` 实例
```html
```
---
```js
new Vue({
el: '#components-demo'
})
```
组件的复用
```html
```
### `data` 必须是一个函数
```js
data: function () {
return {
count: 0
}
}
```
组件的 `data` 选项必须是一个函数
### 向子组件传递数据
```js
Vue.component('blog-post', {
props: ['title'],
template: '{{ title }} '
})
```
当值传递给一个 `prop` `attribute` 的时候,变成了组件实例的一个 `property`
```html
```
### 单个根元素
```js {4}
Vue.component('blog-post', {
props: ['post'],
template: `
`
})
```
---
```html
```
### 监听子组件事件
```js {7}
Vue.component('blog-post', {
props: ['post'],
template: `
{{ post.title }}
Enlarge text
`
})
```
---
```html {3}
```
可以使用 `$emit` 的第二个参数来提供这个值
```html {2}
Enlarge text
```
通过 `$event` 访问到被抛出的这个值
```html {3}
```
如果这个事件处理函数是一个方法
```html {3}
```
那么这个值将会作为第一个参数传入这个方法
```js
methods: {
onEnlargeText: function(enlargeAmount) {
this.postFontSize += enlargeAmount
}
}
```
### 在组件上使用 v-model
```html
```
等价于
```html
```
为了让它正常工作,这个组件内的 \ 必须:
- 将其 `value` attribute 绑定到一个名叫 `value` 的 `prop` 上
- 在其 `input` 事件被触发时,将新的值通过自定义的 `input` 事件抛出
---
```js
Vue.component('custom-input', {
props: ['value'],
template: `
`
})
```
现在 `v-model` 就应该可以在这个组件上完美地工作起来了
```html {2}
```
### 通过插槽分发内容
```html
发生了不好的事情。
```
---
```js {5}
Vue.component('alert-box', {
template: `
Error!
`
})
```
### 动态组件示例
```html
{{ tab }}
```
### 解析 DOM 模板时的注意事项
有些 HTML 元素,诸如 ``、``、`` 和 ``,对于哪些元素可以出现在其内部是有严格限制的。有些元素,诸如 ``、`` 和 ``,只能出现在其它某些特定的元素内部
```html
```
`` 会被作为无效的内容提升到外部
---
如果我们从以下来源使用模板的话,这条限制是不存在的
- 字符串 (例如:template: '...')
- 单文件组件 (.vue)
- `