1)雙向綁定:javascript
-
-
-
<input v-model="message"/>
-
-
-
-
-
-
-
2)渲染列表css
-
-
-
<li v-for="todo in todos">{{todo.text}}</li>
-
-
-
-
-
-
-
-
-
-
-
-
3)處理用戶輸入html
-
-
-
<input v-model='message'/>
-
<button type="button" v-on:click="reverseMessage">反轉</button>
-
-
-
-
-
-
-
-
reverseMessage:function(){
-
this.message=this.message.split('').reverse().join('')
-
-
-
4)綜合小例子:記事本vue
-
-
<input v-model="newTodo" v-on:keyup.enter="addTodo" placeholder="請記錄未完成的計劃" />
-
-
<li v-for="todo in todos">
-
<span>{{todo.text}}</span>
-
<button type="button" v-on:click="removeTodo($index)">X</button>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
var text = this.newTodo.trim();
-
-
this.todos.push({text:text});
-
-
-
-
removeTodo:function(index){
-
this.todos.splice(index,1);
-
-
-
5)插值java
-
-
-
-
-
-
-
<p id="item-{{id}}">html特性</p>
-
-
-
-
-
-
raw_html:'<span>原始的html</span>',
-
-
-
6)綁定表達式webpack
-
-
-
-
<p>{{ok ? 'Yes' : 'No'}}</p>
-
<p>{{message.split('').reverse().join('')}}</p>
-
-
<p>{{name | capitalize}}</p>
-
-
-
-
-
-
-
-
-
-
7)指令web
-
-
-
<a v-bind:href="url" v-on:click="dosomething">指令帶參數</a>
-
-
<a :href="url" @click="dosomething">指令縮寫</a>
-
-
-
-
-
-
-
-
-
-
-
-
8)計算屬性express
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
return this.firstName + ' ' + this.lastName;
-
-
-
var names = newValue.split(' ');
-
this.firstName = names[0];
-
this.lastName = names[names.length-1];
-
-
-
-
9)class與style綁定json
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<p class="static" v-bind:class="{'class-a':isA,'class-b':isB}">綁定class</p>
-
<p class="static" v-bind:class="classObject">綁定class</p>
-
-
<p class="static" v-bind:class="[classA,classB,classC]">綁定class</p>
-
<p class="static" v-bind:class="[classA,{ 'class-b': isB,'class-c': isC}]">綁定class</p>
-
-
<p class="static" v-bind:style="styleObject">綁定style</p>
-
-
<p class="static" v-bind:style="[styleObjectA,styleObjectB]">綁定style</p>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
10)條件渲染api
-
-
<h1 v-if="Math.random() > 0.5">對不起!</h1>
-
-
-
-
-
-
-
-
-
<h1 v-show="isShow">Hello!</h1>
-
-
<custom-component v-show="condition"></custom-component>
-
<p v-show="!condition">這可能也是一個組件</p>
-
-
-
var MyComponent = Vue.extend({
-
template: '<div>A custom component!</div>'
-
-
-
-
Vue.component(
'custom-component', MyComponent);
-
-
-
-
-
-
-
-
-
11)列表渲染
-
-
-
-
<template v-for="item in items" track-by="_uid">
-
<li>{{ parentMessage }} - {{ $index }} - {{ item.message }}</li>
-
<li class="divider"></li>
-
-
-
-
-
<li v-for="(key,val) in object">{{key}} : {{val}}</li>
-
-
-
<span v-for="n in 10">{{ n }}</span>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
{
_uid:'001',message:'香蕉'},
-
{
_uid:'002',message:'橘子'}
-
-
-
-
-
-
-
-
-
-
vm.items.push({
message:'蘋果'},{message:'梨子'});
-
-
-
vm.items=vm.items.filter(
function (item) {
-
return item.message.match(/子/);
-
12)方法與事件處理器
-
-
-
<button type="button" v-on:click="say('Hello',$event)">提交</button>
-
-
-
<a v-on:click.stop="doThis"></a>
-
-
-
<form v-on:submit.prevent="onSubmit"></form>
-
-
-
<a v-on:click.stop.prevent="doThat"></a>
-
-
-
<form v-on:submit.prevent></form>
-
-
-
<div v-on:click.capture="doThis">...</div>
-
-
-
<div v-on:click.self="doThat">...</div>
-
-
-
<input v-on:keyup.enter="submit">
-
-
-
-
-
-
-
alert(msg+
","+event.target.tagName);
-
-
-
-
13)表單控件綁定
-
-
-
-
-
<textarea v-model="message" placeholder="請輸入您的評論"></textarea>
-
-
-
<input type="checkbox" id="checkbox" v-model="checked">
-
<label for="checkbox">{{ checked }}</label>
-
-
-
<input type="checkbox" id="jack" value="馬雲" v-model="checkedNames">
-
<label for="jack">馬雲</label>
-
<input type="checkbox" id="john" value="馬化騰" v-model="checkedNames">
-
<label for="john">馬化騰</label>
-
<input type="checkbox" id="mike" value="李彥宏" v-model="checkedNames">
-
<label for="mike">李彥宏</label>
-
-
<span>選中的值: {{ checkedNames | json }}</span>
-
-
-
<input type="radio" id="one" value="One" v-model="picked">
-
<label for="one">One</label>
-
-
<input type="radio" id="two" value="Two" v-model="picked">
-
<label for="two">Two</label>
-
-
<span>選中的值: {{ picked }}</span>
-
-
-
<select v-model="selected">
-
<option selected>湖北</option>
-
-
-
-
<span>選中的值: {{ selected }}</span>
-
-
-
<select v-model="selecteds" multiple>
-
<option v-for="option in options" v-bind:value="option.value">{{ option.text }}</option>
-
-
-
<span>選中的值: {{ selecteds | json }}</span>
-
-
-
-
-
-
<input type="checkbox" v-model="toggle" v-bind:true-value="a" v-bind:false-value="b"/>
-
<span>選中時的值:{{toggle}}</span>
-
-
-
<input type="radio" v-model="pick" v-bind:value="c">男
-
<input type="radio" v-model="pick" v-bind:value="d">女
-
<span>選中時的值:{{pick}}</span>
-
-
-
<input v-model="msg" lazy>
-
-
<input v-model="age" number>
-
-
<input v-model="msg" debounce="500">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
14)css過渡
-
-
<div v-if="show" transition="expand">Hello1</div>
-
<div v-if="show" :transition="transitionName">Hello2</div>
-
<button type="button" v-on:click="toggle">過渡效果演示</button>
-
-
-
-
transition: all 0.5s ease;
-
-
-
-
-
-
-
-
-
.expand-enter, .expand-leave {
-
-
-
-
-
-
transition: all 0.5s ease;
-
-
-
outline: 0px; color: rgb(209, 154, 102); word-break: break-all;">2df;
-
-
-
.fade-enter, .fade-leave {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
beforeEnter: function (el) {
-
el.textContent =
'beforeEnter'
-
-
-
-
-
afterEnter: function (el) {
-
el.textContent =
'afterEnter'
-
-
beforeLeave: function (el) {
-
el.textContent =
'beforeLeave'
-
-
-
-
-
afterLeave: function (el) {
-
el.textContent =
'afterLeave'
-
-
-
-
15)css自定義過渡(注:與animats.css配合使用)
-
-
<div v-show="ok" class="animated" transition="bounce">只有我最搖擺</div>
-
<button type="button" v-on:click="toggle">演示過渡效果</button>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
enterClass: 'bounceInLeft',
-
leaveClass: 'bounceOutRight'
-
-
-
16)CSS動畫(注:與animats.css配合使用)
-
-
<div v-show="ok" class="animated" transition="bounce">看我變一個</div>
-
<button type="button" v-on:click="toggle">演示動畫效果</button>
-
-
-
-
-
-
-
-
-
-
-
-
animation: bounce-in .5s;
-
-
-
animation: bounce-out .5s;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
17)Javascript過渡
-
-
<p transition='fade'>什麼和什麼?</p>
-
-
-
transition: all 0.5s ease;
-
-
-
-
-
-
-
-
-
-
-
enter: function (el, done) {
-
-
-
-
-
.animate({
opacity: 1 }, 1000, done)
-
-
enterCancelled: function (el) {
-
-
-
leave: function (el, done) {
-
-
$(el).animate({
opacity: 0 }, 1000, done)
-
-
leaveCancelled: function (el) {
-
-
-
18)漸進過渡
-
-
-
-
<li v-for="item in list | filterBy query" transition="staggered" stagger="100">
-
-
-
-
-
-
-
-
-
-
-
-
-
font-family: Helvetica, Arial, sans-serif;
-
-
-
transition: all .5s ease;
-
-
-
-
-
.bounceInLeft, .bounceOutRight {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
var exampleVM = new Vue({
-
-
-
-
-
enterClass:'bounceInLeft',
-
leaveClass: 'bounceOutRight'
-
-
-
-
-
-
-
-
-
-
-
19)組件
-
-
<my-component></my-component>
-
-
-
-
-
-
-
-
-
Vue.component(
'my-component',{
-
template:'<div>A custom component!</div>'
-
-
-
-
-
20)組件局部註冊
-
-
<parent-component></parent-component>
-
-
-
-
-
-
-
template:'<div>A parent component!<my-component></my-component></div>',
-
-
-
-
template:'<div>A child component!</div>'
-
-
-
-
-
Vue.component(
'parent-component', Parent);
-
-
-
-
21)使用props傳遞數據
-
<div id="example" class="demo">
-
<input type="text" v-model="parentMessage"/><br>
-
<child v-bind:my-message="parentMessage"></child>
-
-
<child :msg.sync="parentMessage"></child>
-
-
-
<child :msg.once="parentMessage"></child>
-
-
-
-
-
-
-
-
-
-
-
-
-
parentMessage:'Message from parent'
-
-
-
-
-
template:'<span>{{myMessage}}</span>'
-
-
-
22)prop驗證
-
<div id="example" class="demo">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
propM: [
String, Number],
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
validator: function (value) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
template:'<span>hello world!</span>'
-
-
-
23)父子組件通訊
-
-
<template id="child-template">
-
<input type="text" v-model="msg"/>
-
<button type="button" v-on:click="notify">派發事件</button>
-
-
-
<div id="events-example" class="demo">
-
<p>Messages:{{ messages | json }}</p>
-
<child v-on:child-msg="handleIt"></child>
-
-
-
-
-
-
-
-
-
-
-
-
template:'#child-template',
-
-
-
-
-
-
-
this.$dispatch('child-msg',this.msg);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
24)自定義指令-基礎
<div id="demo" v-demo:hello.a.b="msg"></div>
-
-
-
console.log('demo bound!')
-
-
update: function (value) {
-
-
'name - ' + this.name + '<br>' +
-
'expression - ' + this.expression + '<br>' +
-
'argument - ' + this.arg + '<br>' +
-
'modifiers - ' + JSON.stringify(this.modifiers) + '<br>' +
-
-
-
-
-
-
-
-
-
25)自定義指令-高級選項
-
-
-
<div v-demo-1="{ color: 'white', text: 'hello!' }"></div>
-
-
<div v-demo-2.literal="foo bar baz"></div>
-
-
<my-directive></my-directive>
-
-
<div v-example a="hi"></div>
-
-
Vue.directive(
'demo-1', function(value){
-
console.log(value.color);
-
-
-
Vue.directive(
'demo-2',function(value){
-
-
-
Vue.elementDirective(
'my-directive',{
-
-
-
-
-
Vue.directive(
'example',{
-
-
-
console.log(this.params.a);
-
-
-
-
-
-
this.handler = function () {
-
-
-
-
-
-
this.el.addEventListener('input', this.handler);
-
-
-
this.el.removeEventListener('input', this.handler)
-
-
-
-
-
26)自定義過濾器
-
<div id="demo" class="demo">
-
-
<span v-text="message | reverse"></span><br>
-
<span v-text="message | wrap 'before' 'after'"></span><br>
-
-
<input type="text" v-model="money | currencyDisplay"/>
-
<p>ModelValue:{{money | currencyDisplay}}</p>
-
-
<input v-model="userInput"/>
-
<span>{{msg | concat userInput}}</span>
-
-
-
-
-
-
-
-
-
-
Vue.filter(
'reverse',function(value){
-
return value.split('').reverse().join('');
-
-
Vue.filter(
'wrap',function(value,begin,end){
-
return begin+' '+value+' '+end;
-
-
Vue.filter(
'currencyDisplay',{
-
-
-
-
return '$'+val.toFixed(2)
-
-
-
-
write:
function(val, oldVal) {
-
var number = +val.replace(/[^\d.]/g, '')
-
return isNaN(number) ? 0 : parseFloat(number.toFixed(2))
-
-
-
Vue.filter(
'concat',function(value,input){
-
-
-
-
-
-
-
-
-
-
-
27)混合
-
<div id="demo" class="demo">
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
console.log('hello from mixin!');
-
-
-
-
-
var Component = Vue.extend({
-
-
-
var component = new Component();
-
-
-
28)混合-選項合併
-
<div id="demo" class="demo">
-
-
-
-
-
-
-
-
-
-
-
-
-
console.log('mixin hook called');
-
-
-
-
-
-
conflicting: function () {
-
console.log('from mixin');
-
-
-
-
-
-
-
-
console.log('component hook called');
-
-
-
-
-
-
conflicting: function () {
-
console.log('from self');
-
-
-
-
-
-