1. 程式人生 > >Vue基礎彙總實踐

Vue基礎彙總實踐

1)雙向繫結:

<div id="app">
	<p>{{message}}</p>
	<input v-model="message"/>
</div>
new Vue({
	el:'#app',
	data:{
		message:'Hello vue.js'
	}
})
2)渲染列表
<div id="app">
	<ul>
		<li v-for="todo in todos">{{todo.text}}</li>
	</ul>
</div>
new Vue({
	el:'#app',
	data:{
		todos:[
			{text:'學習vue'},
			{text:'學習Sass'},
			{text:'學習webpack'}
		]
	}
})
3)處理使用者輸入
<div id="app">
	<p>{{message}}</p>
	<input v-model='message'/>
	<button type="button" v-on:click="reverseMessage">反轉</button>
</div>
new Vue({
	el:'#app',
	data:{
		message:'hello world'
	},
	methods:{
		reverseMessage:function(){
			this.message=this.message.split('').reverse().join('')
		}
	}
})
4)綜合小例子:記事本
<div id="app">
	<input v-model="newTodo" v-on:keyup.enter="addTodo" placeholder="請記錄未完成的計劃" />
	<ul>
		<li v-for="todo in todos">
			<span>{{todo.text}}</span>
			<button type="button" v-on:click="removeTodo($index)">X</button>
		</li>
	</ul>
</div>
new Vue({
	el:'#app',
	data:{
		newTodo:'',
		todos:[
			{text:'學習Vue'},
			{text:'學習Sass'},
			{text:'學習webpack'}
		]
	},
	methods:{
		addTodo:function(){
			var text = this.newTodo.trim();
			if(text){
				this.todos.push({text:text});
				this.newTodo='';
			}
		},
		removeTodo:function(index){
			this.todos.splice(index,1);
		}
	}
})
5)插值
<div id="app">
	<!-- 單次文字插值 -->
	<p>{{*message}}</p>
	<!-- 解析真的html字串 -->
	<p>{{{raw_html}}}</p>
	<!-- html特性 -->
	<p id="item-{{id}}">html特性</p>
</div>
new Vue({
	el:'#app',
	data:{
		message:'Hello vue.js',
		raw_html:'<span>原始的html</span>',
		id:'1'
	}
})
6)繫結表示式
<div id="app">
	<!-- javascript表示式 -->
	<p>{{number+1}}</p>
	<p>{{ok ? 'Yes' : 'No'}}</p>
	<p>{{message.split('').reverse().join('')}}</p>
	<!-- 過濾器 -->
	<p>{{name | capitalize}}</p>
</div>
new Vue({
	el:'#app',
	data:{
		number:2,
		ok:false,
		message:'123456789',
		name:'brucee lee'
	}
})
7)指令
<div id="app">
	<!-- 引數 -->
	<a v-bind:href="url" v-on:click="dosomething">指令帶引數</a>
	<!-- v-bind、v-on縮寫 -->
	<a :href="url" @click="dosomething">指令縮寫</a>
</div>
new Vue({
	el:'#app',
	data:{
		url:'http://g.pptv.com'
	},
	methods:{
		dosomething:function(){
			alert(this.url);
		}
	}
})
8)計算屬性
<div id="app">
	<p>a={{a}},b={{b}}</p>
	<p>{{fullName}}</p>
</div>
new Vue({
	el:'#app',
	data:{
		a:1,
		firstName:'Micro',
		lastName:'Jodon'
	},
	computed:{
		b:function(){
			return this.a + 1;
		},
		/*fullName:function(){
			return this.firstName + ' ' + this.lastName;
		}*/
		fullName:{
			get:function(){
				return this.firstName + ' ' + this.lastName;
			},
			set:function(newValue){
				var names = newValue.split(' ');
				this.firstName = names[0];
				this.lastName = names[names.length-1];
			}
		}
	}
})
9)class與style繫結
		.static{
			width: 200px;
			margin: 20px auto;
			height: 25px;
			line-height: 25px;
			text-align: center;
			font-size: 18px;
		}
		.class-a{
			background-color: #f00;
		}
		.class-b{
			color: #fff;
		}
		.class-c{
			padding: 5px 0;
		}
<div id="app">
	<!-- 繫結class:物件語法 -->
	<p class="static" v-bind:class="{'class-a':isA,'class-b':isB}">繫結class</p>
	<p class="static" v-bind:class="classObject">繫結class</p>
	<!-- 繫結class:陣列語法 -->
	<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>
	<!-- 繫結style:物件語法 -->
	<p class="static" v-bind:style="styleObject">繫結style</p>
	<!-- 繫結style:陣列語法 -->
	<p class="static" v-bind:style="[styleObjectA,styleObjectB]">繫結style</p>
</div>
new Vue({
	el:'#app',
	data:{
		classA:'class-a',
		classB:'class-b',
		classC:'class-c',
		isA:true,
		isB:false,
		isC:true,
		classObject:{
			'class-a':true,
			'class-b':true
		},
		styleObject:{
			color:'red',
			fontSize:'13px',
			backgroundColor:'#00f'
		},
		styleObjectA:{
			color:'green',
			fontSize:'16px'
			
		},
		styleObjectB:{
			backgroundColor:'#f0f',
			transform:'rotate(7deg)'
		}
	}
})
10)條件渲染
<div id="app">
	<h1 v-if="Math.random() > 0.5">對不起!</h1>
	<h1 v-else>沒關係</h1>

	<template v-if="ok">
		<h1>標題</h1>
		<p>段落1</p>
		<p>段落2</p>
	</template>

	<h1 v-show="isShow">Hello!</h1>

	<custom-component v-show="condition"></custom-component>
	<p v-show="!condition">這可能也是一個元件</p>
</div>
// 定義
var MyComponent = Vue.extend({
  template: '<div>A custom component!</div>'
});

// 註冊
Vue.component('custom-component', MyComponent);
new Vue({
	el:'#app',
	data:{
		ok:true,
		isShow:false,
		condition:true
	},

})
11)列表渲染
<div id="app">
	<!-- 陣列v-for -->
	<ul>
		<template v-for="item in items" track-by="_uid">
			<li>{{ parentMessage }} - {{ $index }} - {{ item.message }}</li>
			<li class="divider"></li>
		</template>
	</ul>
	<!-- 物件v-for -->
	<ul>
		<li v-for="(key,val) in object">{{key}} : {{val}}</li>
	</ul>
	<!-- 值域v-for -->
	<span v-for="n in 10">{{ n }}</span>
</div>
		ul{
			list-style: none;
			width: 150px;
		}
		.divider{
			height: 2px;
			background-color: #00f;
		}
		span{
			padding: 0 2px;
		}
var vm=new Vue({
	el:'#app',
	data:{
		parentMessage:'水果',
		items:[
			{ _uid:'001',message:'香蕉'},
			{ _uid:'002',message:'橘子'}
		],
		object:{
			FirstName: 'John',
      		LastName: 'Doe',
      		Age: 30
		}
	}
});
//變異方法:push()、pop()、shift()、unshift()、splice()、sort()、reverse()
vm.items.push({message:'蘋果'},{message:'梨子'});//推入兩項
vm.items.shift();//取出第一項
//非變異方法:filter(), concat(), slice(),可用替換陣列來修改原陣列
vm.items=vm.items.filter(function (item) {
  return item.message.match(/子/);
})
12)方法與事件處理器
<div id="app">
	<!-- 內聯語句處理器 -->
	<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>

	<!-- 新增事件偵聽器時使用 capture 模式 -->
	<div v-on:click.capture="doThis">...</div>

	<!-- 只當事件在該元素本身(而不是子元素)觸發時觸發回撥 -->
	<div v-on:click.self="doThat">...</div>

	<!-- 按鍵修飾符 -->
	<input v-on:keyup.enter="submit">

</div>
var vm=new Vue({
	el:'#app',
	methods:{
		say:function(msg,event){
			alert(msg+","+event.target.tagName);
			 event.preventDefault();
		}
	}
});
13)表單控制元件繫結
<div id="app">
	<!-- 多行文字 -->
	<span>這是您的評論:</span>
	<p>{{message}}</p>
	<textarea v-model="message" placeholder="請輸入您的評論"></textarea>
	<br>
	<!-- 單選框 -->
	<input type="checkbox" id="checkbox" v-model="checked">
	<label for="checkbox">{{ checked }}</label>
	<br>
	<!-- 多個單選框 -->
	<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>
	<br>
	<span>選中的值: {{ checkedNames | json }}</span>
	<br>
	<!-- 單選鈕 -->
	<input type="radio" id="one" value="One" v-model="picked">
	<label for="one">One</label>
	<br>
	<input type="radio" id="two" value="Two" v-model="picked">
	<label for="two">Two</label>
	<br>
	<span>選中的值: {{ picked }}</span>
	<br>
	<!-- 下拉列表單選 -->
	<select v-model="selected">
	  <option selected>湖北</option>
	  <option>北京</option>
	  <option>上海</option>
	</select>
	<span>選中的值: {{ selected }}</span>
	<br>
	<!-- 下拉列表多選 -->
	<select v-model="selecteds" multiple>
	  	<option v-for="option in options" v-bind:value="option.value">{{ option.text }}</option>
	</select>
	<br>
	<span>選中的值: {{ selecteds | json }}</span>
	<br>
	
	<!--繫結動態值到Vue例項-->

	<!-- 選中時為a,未選中時為b -->
	<input type="checkbox" v-model="toggle" v-bind:true-value="a" v-bind:false-value="b"/>
	<span>選中時的值:{{toggle}}</span>
	<br>
	
	<input type="radio" v-model="pick" v-bind:value="c">男
	<input type="radio" v-model="pick" v-bind:value="d">女
	<span>選中時的值:{{pick}}</span>

	<!-- 在 "change" 而不是 "input" 事件中更新 -->
	<input v-model="msg" lazy>
	<!-- 設定轉換為數值型別 -->
	<input v-model="age" number>
	<!-- 設定延時 -->
	<input v-model="msg" debounce="500">
</div>
var vm=new Vue({
	el:'#app',
	data: {
    	checkedNames: [],
    	options:[
    		{text:'南京',value:'南京'},
    		{text:'蘇州',value:'蘇州'},
    		{text:'無錫',value:'無錫'}
    	],
    	a:'選中',
    	b:'未選中',
    	c:'男',
    	d:'女'
  }
});
14)css過渡
<div id="app">
	<div v-if="show" transition="expand">Hello1</div>
	<div v-if="show" :transition="transitionName">Hello2</div>
	<button type="button" v-on:click="toggle">過渡效果演示</button>
</div>
			/* 必需 */
	.expand-transition {
	  transition: all 0.5s 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;
	}
	.fade-transition{
		transition: all 0.5s ease;
		height: 30px;
	  	padding: 10px;
	  	background-color: #2df;
	  	overflow: hidden;
	}
	.fade-enter, .fade-leave {
	  padding: 0 10px;
	  opacity: 0.5;
	}
var vm=new Vue({
	el:'#app',
	data: {
    	show:true,
    	transitionName:'fade'
  	},
  	methods:{
  		toggle:function(){
  			if(this.show){
  				this.show=false;
  			}else{
  				this.show=true;
  			}
  			
  		}
  	},
  	transitions: {
	    expand: {
	      beforeEnter: function (el) {
	        el.textContent = 'beforeEnter'
	      },
	      enter: function (el) {
	        el.textContent = 'enter'
	      },
	      afterEnter: function (el) {
	        el.textContent = 'afterEnter'
	      },
	      beforeLeave: function (el) {
	        el.textContent = 'beforeLeave'
	      },
	      leave: function (el) {
	        el.textContent = 'leave'
	      },
	      afterLeave: function (el) {
	        el.textContent = 'afterLeave'
	      }
	    }
  	}
});
15)css自定義過渡(注:與animats.css配合使用
<div id="app">
	<div v-show="ok" class="animated" transition="bounce">只有我最搖擺</div>
	<button type="button" v-on:click="toggle">演示過渡效果</button>
</div>
	.animated{
		width: 150px;
		background-color: #2df;
		height: 30px;
	  	padding: 10px;
	}
var vm=new Vue({
	el:'#app',
	data: {
    	ok:true
  	},
  	methods:{
  		toggle:function(){
  			if(this.ok){
  				this.ok=false;
  			}else{
  				this.ok=true;
  			}
  		}
  	},
  	transitions: {
	    bounce: {
	      enterClass: 'bounceInLeft',
  		  leaveClass: 'bounceOutRight'
	    }
  	}	
});
16)CSS動畫(注:與animats.css配合使用
<div id="app">
	<div v-show="ok" class="animated" transition="bounce">看我變一個</div>
	<button type="button" v-on:click="toggle">演示動畫效果</button>
</div>
	.animated{
		width: 150px;
		background-color: #2df;
		height: 30px;
	  	padding: 10px;
	}
	.bounce-transition {
	  display: inline-block; /* 否則 scale 動畫不起作用 */
	}
	.bounce-enter {
	  animation: bounce-in .5s;
	}
	.bounce-leave {
	  animation: bounce-out .5s;
	}
	@keyframes bounce-in {
	  0% {
	    transform: scale(0);
	  }
	  50% {
	    transform: scale(1.5);
	  }
	  100% {
	    transform: scale(1);
	  }
	}
	@keyframes bounce-out {
	  0% {
	    transform: scale(1);
	  }
	  50% {
	    transform: scale(1.5);
	  }
	  100% {
	    transform: scale(0);
	  }
	}
	
var vm=new Vue({
	el:'#app',
	data: {
    	ok:true
  	},
  	methods:{
  		toggle:function(){
  			if(this.ok){
  				this.ok=false;
  			}else{
  				this.ok=true;
  			}
  		}
  	}
});
17)Javascript過渡
<div id="app">
	<p transition='fade'>什麼和什麼?</p>
</div>
	.fade-transition{
		transition: all 0.5s ease;
		height: 30px;
	  	padding: 10px;
	  	background-color: #2df;
	  	overflow: hidden;
	}
var vm=new Vue({
	el:'#app'
});
vm.transition('fade', {
  css: false,
  enter: function (el, done) {
    // 元素已被插入 DOM
    // 在動畫結束後呼叫 done
    $(el)
      .css('opacity', 0)
      .animate({ opacity: 1 }, 1000, done)
  },
  enterCancelled: function (el) {
    $(el).stop()
  },
  leave: function (el, done) {
    // 與 enter 相同
    $(el).animate({ opacity: 0 }, 1000, done)
  },
  leaveCancelled: function (el) {
    $(el).stop()
  }
})
18)漸進過渡
<div id="example-1">
    <input v-model="query">
    <ul>
        <li v-for="item in list | filterBy query" transition="staggered" stagger="100">
            {{item.msg}}
        </li>
    </ul>
</div>
        #example-1{
            width:200px;
            margin:20px auto;
            font-size:14px;
            color:#ff6600;
        }
        ul {
            padding-left: 0;
            font-family: Helvetica, Arial, sans-serif;
        }
        .staggered-transition {
            transition: all .5s ease;
            overflow: hidden;
            margin: 0;
            height: 25px;
        }
        .bounceInLeft, .bounceOutRight {
            opacity: 0;
            height: 0;
        }
    var exampleData={
        query: '',
        list: [
            { msg: 'Bruce Lee' },
            { msg: 'Jackie Chan' },
            { msg: 'Chuck Norris' },
            { msg: 'Jet Li' },
            { msg: 'Kung Fury' }
        ]
    };
    var exampleVM = new Vue({
        el:'#example-1',
        data:exampleData,
        transitions:{
            staggered:{
                enterClass:'bounceInLeft',
                leaveClass: 'bounceOutRight'
            }
        }
    });

/*    exampleVM.transition('stagger', {
        stagger: function (index) {
            // 每個過渡專案增加 50ms 延時
            // 但是最大延時限制為 300ms
            return Math.min(300, index * 50)
        }
    })*/
19)元件
<div id="example">
    <my-component></my-component>
</div>
//定義
/*var myComponent=Vue.extend({
    template:'<div>A custom component!</div>'
});*/
//註冊
//Vue.component('my-component',myComponent);
//在一個步驟中定義與註冊
Vue.component('my-component',{
    template:'<div>A custom component!</div>'
});
//建立根例項
new Vue({
    el:'#example'
});
20)元件區域性註冊
<div id="example">
    <parent-component></parent-component>
</div>
//定義
/*var Child=Vue.extend({
    template:'<div>A child component!</div>'
});*/
var Parent=Vue.extend({
    template:'<div>A parent component!<my-component></my-component></div>',
    components:{
        // <my-component> 只能用在父元件模板內
        'my-component':{
            template:'<div>A child component!</div>'
        }
    }
});
// 註冊父元件
Vue.component('parent-component', Parent);
//建立根例項
new Vue({
    el:'#example'
});
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>
</div>
        .demo{
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 25px 35px;
            margin-bottom: 40px;
            font-size: 1.2em;
            line-height: 1.5em;
        }
new Vue({
    el:'#example',
    data:{
        parentMessage:'Message from parent'
    },
    components:{
        child:{
            props:['myMessage'],
            template:'<span>{{myMessage}}</span>'
        }
    }
});
22)prop驗證
<div id="example" class="demo">
    <child></child>
</div>
        .demo{
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 25px 35px;
            margin-bottom: 40px;
            font-size: 1.2em;
            line-height: 1.5em;
        }
new Vue({
    el:'#example',
    components:{
        child:{
            props: {
                // 基礎型別檢測 (`null` 意思是任何型別都可以)
                propA: Number,
                // 多種型別 (1.0.21+)
                propM: [String, Number],
                // 必需且是字串
                propB: {
                    type: String,
                    required: true
                },
                // 數字,有預設值
                propC: {
                    type: Number,
                    default: 100
                },
                // 物件/陣列的預設值應當由一個函式返回
                propD: {
                    type: Object,
                    default: function () {
                        return { msg: 'hello' }
                    }
                },
                // 指定這個 prop 為雙向繫結
                // 如果繫結型別不對將丟擲一條警告
                propE: {
                    twoWay: true
                },
                // 自定義驗證函式
                propF: {
                    validator: function (value) {
                        return value > 10
                    }
                },
                // 轉換函式(1.0.12 新增)
                // 在設定值之前轉換值
                propG: {
                    coerce: function (val) {
                        return val + ''; // 將值轉換為字串
                    }
                },
                propH: {
                    coerce: function (val) {
                        return JSON.parse(val); // 將 JSON 字串轉換為物件
                    }
                }
            },
            template:'<span>hello world!</span>'
        }
    }
});
23)父子元件通訊
<!--子元件模板-->
<template id="child-template">
    <input type="text" v-model="msg"/>
    <button type="button" v-on:click="notify">派發事件</button>
</template>
<!--父元件模板-->
<div id="events-example" class="demo">
<p>Messages:{{ messages | json }}</p>
    <child v-on:child-msg="handleIt"></child>
</div>
        .demo{
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 25px 35px;
            margin-bottom: 40px;
            font-size: 1.2em;
            line-height: 1.5em;
        }
    // 註冊子元件,將當前訊息派發出去
    Vue.component('child',{
        template:'#child-template',
        data:function(){
            return {msg:'hello'}
        },
        methods:{
            notify:function(){
                if(this.msg.trim()){
                    this.$dispatch('child-msg',this.msg);
                    this.msg='';
                }
            }
        }
    });
    // 初始化父元件,收到訊息時將事件推入一個數組
    var parent=new Vue({
        el:'#events-example',
        data:{
            messages:[]
        },
        methods:{
            handleIt:function(msg){
                this.messages.push(msg);
            }
        }
        // 在建立例項時 `events` 選項簡單地呼叫 `$on`
        /*events:{
            'child-msg':function(msg){
                this.messages.push(msg);
            }
        }*/
    })
24)自定義指令-基礎
<div id="demo" v-demo:hello.a.b="msg"></div>
    Vue.directive('demo', {
        bind: function () {
            console.log('demo bound!')
        },
        update: function (value) {
            this.el.innerHTML =
                    'name - '       + this.name + '<br>' +
                    'expression - ' + this.expression + '<br>' +
                    'argument - '   + this.arg + '<br>' +
                    'modifiers - '  + JSON.stringify(this.modifiers) + '<br>' +
                    'value - '      + value
        }
    });
    var demo = new Vue({
        el: '#demo',
        data: {
            msg: 'hello!'
        }
    })
25)自定義指令-高階選項
<div id="demo" >
    <!--物件字面量-->
    <div v-demo-1="{ color: 'white', text: 'hello!' }"></div>
    <!--字面修飾符-->
    <div v-demo-2.literal="foo bar baz"></div>
    <!--元素指令-->
    <my-directive></my-directive>
    <!--高階選項-params-->
    <div v-example a="hi"></div>
</div>
    Vue.directive('demo-1', function(value){
        console.log(value.color);
        console.log(value.text);
    });
    Vue.directive('demo-2',function(value){
        console.log(value);
    });
    Vue.elementDirective('my-directive',{
        bind:function(){
            console.log(this.el);
        }
    });
    Vue.directive('example',{
        params:['a'],
        bind:function(){
            console.log(this.params.a);
        }
    });
    Vue.directive('two', {
        twoWay: true,
        bind: function () {
            this.handler = function () {
                // 將資料寫回 vm
                // 如果指令這樣繫結 v-example="a.b.c"
                // 它將用給定值設定 `vm.a.b.c`
                this.set(this.el.value)
            }.bind(this);
            this.el.addEventListener('input', this.handler);
        },
        unbind: function () {
            this.el.removeEventListener('input', this.handler)
        }
    });
var vm=new Vue({
        el: '#demo'
    });
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>
</div>
        .demo{
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 25px 35px;
            margin-bottom: 40px;
            font-size: 1.2em;
            line-height: 1.5em;
        }
    Vue.filter('reverse',function(value){
        return value.split('').reverse().join('');
    });
    Vue.filter('wrap',function(value,begin,end){
       return begin+' '+value+' '+end;
    });
    Vue.filter('currencyDisplay',{
        // model -> view
        // 在更新 `<input>` 元素之前格式化值
        read:function(val){
            return '$'+val.toFixed(2)
        },
        // view -> model
        // 在寫回資料之前格式化值
        write: function(val, oldVal) {
            var number = +val.replace(/[^\d.]/g, '')
            return isNaN(number) ? 0 : parseFloat(number.toFixed(2))
        }
    });
    Vue.filter('concat',function(value,input){
        return value+input;
    })
    new Vue({
        el:'#demo',
        data:{
            message:'abc',
            money:123.45,
            msg:'hello',
            userInput:'hi'
        }
    });
27)混合
<div id="demo" class="demo">

</div>
        .demo{
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 25px 35px;
            margin-bottom: 40px;
            font-size: 1.2em;
            line-height: 1.5em;
        }
    var myMixin={
        created:function(){
            this.hello();
        },
        methods:{
            hello:function(){
                console.log('hello from mixin!');
            }
        }
    };
    // 定義一個元件,使用這個混合物件
    var Component = Vue.extend({
        mixins: [myMixin]
    });
    var component = new Component();
    new Vue({
        el:'#demo'
    });
28)混合-選項合併
<div id="demo" class="demo">

</div>
        .demo{
            border: 1px solid #eee;
            border-radius: 2px;
            padding: 25px 35px;
            margin-bottom: 40px;
            font-size: 1.2em;
            line-height: 1.5em;
        }
var mixin={
    created:function(){
        console.log('mixin hook called');
    },
    methods: {
        foo: function () {
          console.log('foo');
        },
        conflicting: function () {
          console.log('from mixin');
        }
    }
};
var vm=new Vue({
    el:'#demo',
    mixins:[mixin],
    created:function(){
        console.log('component hook called');
    },
    methods:{
        bar: function () {
          console.log('bar');
        },
        conflicting: function () {
          console.log('from self');
        }
    }
});
vm.foo();
vm.bar();
vm.conflicting();//元件優先