vue應該是前端主流框架中的集大成者,它吸取了knockout,angular,react設置avalon的經驗,支持各種模式寫法,入門很簡單,從本章開始,會記錄學習vue中的點點滴滴,以筆記的形式形成博文。
1、Hello World
和任何框架一樣,使用前必先引入,我們這里直接使用cdn資源,創建index.html,編寫如下代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>demo1</title>
<script src="https://cdn.bootcss.com/vue/2.4.1/vue.js"></script>
</head>
<body>
<div id="app">
{{message}}
</div>
<script>
new Vue({
el:'#app',
data:{
message:'hello world'
}
})
</script>
</body>
</html>
雙擊文件運行:

咱們分析一下上述代碼,一句簡單的{{message}},就把數據綁定到到了dom中,我不會說好神奇,因為我熟悉angular,我知道angular也是簡單一個指令就可以做到,這里和angular在使用上做一個類比,可以看到,在當前的代碼中創建Vue對象的時候,傳遞的參數{el:'#id',data:{message:'hellow'}},其中el是vue掛載的元素,也就是作用的范圍和anglar中ng-app的概念類似,都是創建一個根作用域,data對象可以類比angular中的$scope,$scope對象在angular中是連接controller和view的橋梁,那么data對象就是代理vue對象中數據和template的橋梁。
2、TODO LIST
由于有angular的經驗,不會按部就班的過vue的文檔,那樣也沒什么意思,這里以todolist作為Hello world的延伸,由于和angular類似的數據驅動的特點,我們不需要關注如何操作dom,我們只要設計viewmode即可,todolist,首先我們要有todo item ,所以
var todoItem=function(title,desc){
this.title=title;
this.desc=desc;
}
另外todolist 是包含CURD的,所以我們需要一個表單,用來新增todoItem,基于此修改index.html,添加表單部分:
<div id="app">
<form>
<input type="text" v-model="title"> <br>
<input type="text" v-model="desc"> <br/>
<input type="button" value="add todo item" v-on:click="addItem()" />
</form>
</div>
對上述代碼做一下簡單說明:v-model類似angular中ng-model,實現雙向數據綁定,當然這里都是語法糖,vue內部做了很多工作,和angular2+中的[(ngModel)]類似,通過屬性和事件實現了雙向綁定效果,v-on:click="addItem()" ,其中v-on:是事件綁定指令,后面click是參數,可以縮寫為@click="addItem()" ,可以類比angular中ng-click。
接下來,繼續完善我們的viewmodel,todoitem已經設計完成,現在我們增加todolist結構,todolist中包含多個todoitem,是todoitem集合,在javascript中,體現為Array,基于此修改我們的代碼,如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>demo1</title>
<script src="https://cdn.bootcss.com/vue/2.4.1/vue.js"></script>
</head>
<body>
<div id="app">
<form>
title:<input type="text" v-model="title"> <br>
desc:<input type="text" v-model="desc"> <br/>
<input type="button" value="add todo item" v-on:click="addItem()" />
</form>
</div>
<script>
var TodoItem = function (title, desc) {
this.title = title;
this.desc = desc;
}
new Vue({
el: '#app',
data: {
todolist:[],
title:'',
desc:''
},
methods:{
addItem:function(){
this.todolist.push(new TodoItem(this.title,this.desc))
this.title=this.desc='';
console.log(JSON.stringify(this.todolist));
}
}
})
</script>
</body>
</html>
上述代碼中出現了新的屬性methods,和angular中不同,angular中事件也是綁定在$scope對象中的,只不過值是function而已,在vue中,對事件綁定和屬性綁定進行了區分,分別使用data和methods代理,這樣也在邏輯上更清晰,指責上更單一,所以事件綁定的回調函數都代理在methods中。
刷新頁面,輸入表單項,打開控制臺可以看到輸出項,運行結果入下圖所示:

結果和我們預期的一樣,接著我們把結果以列表的形式渲染出來,在angular中,我們一般通過ng-repeat指令,實現列表渲染,那么在vue中,有沒有類似的指令呢,查文檔發現的確有一指令叫做v-for,用法和作用都和ng-repeat類似,基于ng-repeat經驗,我們使用v-for對todolist進行渲染,修改代碼如下:
<table border="1">
<tr>
<th>title</th>
<th>desc</th>
</tr>
<tr v-for="todoItem in todolist">
<td>{{todoItem.title}}</td>
<td>{{todoItem.desc}}</td>
</tr>
</table>
刷新運行,在表單中輸入后,點擊add todo item,向數組添加元素,及動態刷新了列表:

有添加就有刪除,接下來,我們列表中,增加刪除操作,和所有mvvm框架一樣,我們考慮的出發點一定要規避dom,一定要從數據驅動UI的方式來思考,如果刪掉UI項,那么根據數據驅動UI的理念那么就是刪掉數組項,框架會自動幫我們處理dom,基于此修改代碼如下:
<table border="1">
<tr>
<th>title</th>
<th>desc</th>
<th></th>
</tr>
<tr v-for="(todoItem,index) in todolist">
<td>{{todoItem.title}}</td>
<td>{{todoItem.desc}}</td>
<td><input type="button" value="remove" @click="remove(index)" /></td>
</tr>
</table>
如果按照我們以前angular的使用經驗,這里增加的方式有些區別,angular在ng-repeat中有內置變量$index,所以在事件處理上,我們就會通過$index作為數組項索引,事件綁定也會類似ng-click="remove($index)",在vue中就有些區別了,但是卻符合數組遍歷的方式,大家應該知道數組方法,比如map等,參數是一個function,其中包含兩個參數,第一個參數是value,第二個參數是index是一個道理,從這一點上說,這樣寫很符合道理,因為這本身就是一個循環遍歷,同樣vue對象methods中添加remove方法。
new Vue({
el: '#app',
data: {
todolist:[],
title:'',
desc:''
},
methods:{
addItem:function(){
this.todolist.push(new TodoItem(this.title,this.desc))
this.title=this.desc='';
},
remove:function(index){
this.todolist.splice(index,1);
}
}
})
刷新運行頁面:

完整代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>demo1</title>
<script src="https://cdn.bootcss.com/vue/2.4.1/vue.js"></script>
<style>
table{
margin-top: 5px;
width:300px;
border-collapse: collapse;
border: 1px solid #ccc;
}
table > tr>th,table>tr>td{
height: 25px;
line-height: 25px;
}
</style>
</head>
<body>
<div id="app">
<form>
title:<input type="text" v-model="title"> <br>
desc:<input type="text" v-model="desc"> <br/>
<input type="button" value="add todo item" v-on:click="addItem()" />
</form>
<table border="1">
<tr>
<th>title</th>
<th>desc</th>
<th></th>
</tr>
<tr v-for="(todoItem,index) in todolist">
<td>{{todoItem.title}}</td>
<td>{{todoItem.desc}}</td>
<td><input type="button" value="remove" @click="remove(index)" /></td>
</tr>
</table>
</div>
<script>
var TodoItem = function (title, desc) {
this.title = title;
this.desc = desc;
}
new Vue({
el: '#app',
data: {
todolist:[],
title:'',
desc:''
},
methods:{
addItem:function(){
this.todolist.push(new TodoItem(this.title,this.desc))
this.title=this.desc='';
},
remove:function(index){
this.todolist.splice(index,1);
}
}
})
</script>
</body>
</html>
今天就先到這里,增刪改查,查詢和修改還沒有,放在(二)中進行添加吧,敬請期待。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。