一、v-bind 缩写
<!-- 完全语法 -->
<a v-bind:href="url"></a>
<!-- 缩写 -->
<a :href="url"></a>
<!-- 完全语法 -->
<button v-bind:disabled="someDynamicCondition">Button</button>
<!-- 缩写 -->
<button :disabled="someDynamicCondition">Button</button>
二、v-on 缩写
<!-- 完全语法 -->
<a v-on:click="doSomething"></a>
<!-- 缩写 -->
<a @click="doSomething"></a>
三、过滤器
{{ message | capitalize }}
四、前提衬着
v-if
<h1 v-if="ok">Yes</h1>
<h1 v-else>No</h1>
<div v-if="Math.random() > 0.5">
Sorry
</div>
<div v-else>
Not sorry
</div>
template-v-if
<template v-if="ok">
<h1>Title</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</template>
v-show
<h1 v-show="ok">Hello!</h1>
五、列表衬着 for
v-for
<ul id="example-1">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
var example1 = new Vue({
el: '#example-1',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
});
<ul id="example-2">
<li v-for="item in items">
{{ parentMessage }} - {{ $index }} - {{ item.message }}
</li>
</ul>
var example2 = new Vue({
el: '#example-2',
data: {
parentMessage: 'Parent',
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
});
数组更改检测
Vue.js 包装了被视察数组的变异要领,故它们能触发视图更新。被包装的要领有:push(), pop(), shift(), unshift(), splice(), sort(), reverse()
example1.items.push({ message: 'Baz' });
example1.items = example1.items.filter(function (item) {
return item.message.match(/Foo/);
});
template-v-for
<ul>
<template v-for="item in items">
<li>{{ item.msg }}</li>
<li class="divider"></li>
</template>
</ul>
对象 v-for
<ul id="repeat-object" class="demo">
<li v-for="value in object">
{{ $key }} : {{ value }}
</li>
</ul>
new Vue({
el: '#repeat-object',
data: {
object: {
FirstName: 'John',
LastName: 'Doe',
Age: 30
}
}
});
值域 v-for
<div>
<span v-for="n in 10">{{ n }} </span>
</div>
六、要领与事宜处理器
要领处理器
<div id="example">
<button v-on:click="greet">Greet</button>
</div>
var vm = new Vue({
el: '#example',
data: {
name: 'Vue.js'
},
// 在 `methods` 对象中定义要领
methods: {
greet: function (event) {
// 要领内 `this` 指向 vm
alert('Hello ' + this.name + '!')
// `event` 是原生 DOM 事宜
alert(event.target.tagName)
}
}
})
// 也能够在 JavaScript 代码中挪用要领
vm.greet(); // -> 'Hello Vue.js!'
内联语句处理器
<div id="example-2">
<button v-on:click="say('hi')">Say Hi</button>
<button v-on:click="say('what')">Say What</button>
</div>
new Vue({
el: '#example-2',
methods: {
say: function (msg) {
alert(msg)
}
}
});
偶然也须要在内联语句处理器中接见原生 DOM 事宜。能够用特别变量 $event 把它传入要领
<button v-on:click="say('hello!', $event)">Submit</button>
methods: {
say: function (msg, event) {
// 如今我们能够接见原生事宜对象
event.preventDefault()
}
};
## 事宜修饰符
<!-- 阻挠单击事宜冒泡 -->
<a v-on:click.stop="doThis"></a>
<!-- 提交事宜不再重载页面 -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- 修饰符能够串连 -->
<a v-on:click.stop.prevent="doThat">
<!-- 只要修饰符 -->
<form v-on:submit.prevent></form>
## 按键修饰符
<!-- 只要在 keyCode 是 13 时挪用 vm.submit() -->
<input v-on:keyup.13="submit">
<!-- 同上 -->
<input v-on:keyup.enter="submit">
<!-- 缩写语法 -->
<input @keyup.enter="submit">
悉数的按键别号:enter,tab,delete,esc,space,up,down,left,right
## 其他实例
new Vue({
el: '#demo',
data: {
newLabel: '',
stats: stats
},
methods: {
add: function (e) {
e.preventDefault()
if (!this.newLabel) {
return;
}
this.stats.push({
label: this.newLabel,
value: 100
});
this.newLabel = '';
},
remove: function (stat) {
if (this.stats.length > 3) {
this.stats.$remove(stat); // 注重这里的$remove
} else {
alert('Can\'t delete more!')
}
}
}
});
七、过渡
CSS 过渡
<div v-if="show" transition="expand">hello</div>
然后为 .expand-transition, .expand-enter 和 .expand-leave 增加 CSS 划定规矩:
/* 必须 */
.expand-transition {
transition: all .3s ease;
height: 30px;
padding: 10px;
background-color: #eee;
overflow: hidden;
}
/* .expand-enter 定义进入的最先状况 */
/* .expand-leave 定义脱离的完毕状况 */
.expand-enter, .expand-leave {
height: 0;
padding: 0 10px;
opacity: 0;
}
你能够在统一元素上经由过程动态绑定完成差别的过渡:
<div v-if="show" :transition="transitionName">hello</div>
new Vue({
el: '...',
data: {
show: false,
transitionName: 'fade'
}
}
别的,能够供应 JavaScript 钩子:
Vue.transition('expand', {
beforeEnter: function (el) {
el.textContent = 'beforeEnter'
},
enter: function (el) {
el.textContent = 'enter'
},
afterEnter: function (el) {
el.textContent = 'afterEnter'
},
enterCancelled: function (el) {
// handle cancellation
},
beforeLeave: function (el) {
el.textContent = 'beforeLeave'
},
leave: function (el) {
el.textContent = 'leave'
},
afterLeave: function (el) {
el.textContent = 'afterLeave'
},
leaveCancelled: function (el) {
// handle cancellation
}
});