vue干貨第二集
Vue和angular的區(qū)別
- vue是一個mvvm框架(庫),和angular類似易阳,比較容易上手附较,小巧
- vue:
■ 簡單,易學 中文
■ 指令以v-xxx
■ 一片html代碼配合上json潦俺,在new出來一個vue的實例
■ 一個個人維護項目
■ 適合:移動端項目拒课,小巧
■ vue的發(fā)展勢頭很猛徐勃,GitHub上的star數(shù)量已經(jīng)超越angular - angular:
■ 上手難,框架大一些
■ 指令以ng-xxx
■ 所有屬性和方法都掛到¥scope身上
■ angular由Google維護
■ 適合:pc端項目
*共同點:不兼容低版本IE
常用指令:
- 指令的含義:擴展html標簽的功能,屬性
- angular常用的指令:ng-model(放在表單的input元素上), ng-controller
*對應的vue也有常用的指令:v-model: 一般用在表單元素(input)上 雙向數(shù)據(jù)綁定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>v-model指令</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'body',
data:{
msg:'welcome vue'
}
});
};
</script>
</head>
<body>
<input type="text" v-model="msg">
<br>
{{msg}}
</body>
</html>
- v-for 循環(huán)數(shù)組指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>循環(huán)數(shù)組</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
arr:['apple','banana','orange','pear'],
json:{a:'apple',b:'banana',c:'orange'}
}
});
};
</script>
</head>
<body>
<div id="box">
<ul>
<li v-for="value in arr">
{{value}}
</li>
</ul>
</div>
</body>
</html>
- 如何循環(huán)json文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>循環(huán)json文件</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
arr:['apple','banana','orange','pear'],
json:{a:'apple',b:'banana',c:'orange'}
}
});
};
</script>
</head>
<body>
<div id="box">
<ul>
<li v-for="value in arr">
{{value}} {{$index}}
</li>
</ul>
<hr>
<ul>
<li v-for="value in json">
{{value}} {{$index}} {{$key}}
</li>
</ul>
<hr>
<ul>
<li v-for="(k,v) in json">
{{k}} {{v}} {{$index}} {{$key}}
</li>
</ul>
</div>
</body>
</html>
- 事件:在angularjs中是ng-click早像,在vue里面的基礎事件 : v-on:click='函數(shù)'
添加一個按鈕僻肖,點擊按鈕時在頁面中添加一個tomato
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>基礎事件</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{ //數(shù)據(jù)
arr:['apple','banana','orange','pear'],
json:{a:'apple',b:'banana',c:'orange'}
},
methods:{
add:function(){
this.arr.push('tomato');
}
}
});
};
</script>
</head>
<body>
<div id="box">
<input type="button" value="按鈕" v-on:click="add()">
<br>
<ul>
<li v-for="value in arr">
{{value}}
</li>
</ul>
</div>
</body>
</html>
還有一些其他的事件:
v-on:mouseover
v-on:mouseout
v-on:mousedown
v-on:dblclick
- v-show 顯示隱藏:點擊按鈕div消失的demo,通過按鈕的點擊控制v-show的屬性卢鹦,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>顯示隱藏</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{ //數(shù)據(jù)
a:true
}
});
};
</script>
</head>
<body>
<div id="box">
<input type="button" value="按鈕" v-on:click="a=false">
<div style="width:100px; height:100px; background: red" v-show="a">
</div>
</div>
</body>
</html>
- vue 事件簡寫
v-on:click 等價 @click
*事件冒泡
阻止冒泡:cancelBubble
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阻止冒泡</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
},
methods:{
show:function(ev){
alert(1);
ev.cancelBubble=true;
},
show2:function(){
alert(2);
}
}
});
};
</script>
</head>
<body>
<div id="box">
<div @click="show2()">
<input type="button" value="按鈕" @click="show($event)">
</div>
</div>
</body>
</html>
方法2: click.stop 推薦M卧唷!冀自!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阻止冒泡</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
},
methods:{
show:function(){
alert(1);
},
show2:function(){
alert(2);
}
}
});
};
</script>
</head>
<body>
<div id="box">
<div @click="show2()">
<input type="button" value="按鈕" @click.stop="show()">
</div>
</div>
</body>
</html>
阻止默認行為:
1.preventDefault
2.contextDefault.prevent推薦H嘀伞!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阻止默認</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
},
methods:{
show:function(ev){
alert(1);
ev.preventDefault();
}
}
});
};
</script>
</head>
<body>
<div id="box">
<input type="button" value="按鈕" @contextmenu="show($event)">
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阻止默認事件</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<style>
</style>
<script src="vue.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
},
methods:{
show:function(){
alert(1);
}
}
});
};
</script>
</head>
<body>
<div id="box">
<input type="button" value="按鈕" @contextmenu.prevent="show()">
</div>
</body>
</html>
常用的鍵 :簡介的形式
回車@keyup/keydown.enter 熬粗,
上@keyup/keydown.up搀玖,
下@keyup/keydown.down,
左@keyup/keydown.left驻呐,
右@keyup/keydown.right 灌诅,