第一次接觸神奇的前端框架vue.js
前言
入手2016最火前端框架之一vue.js。大概從網(wǎng)上找了些資料看了下vue.js,從網(wǎng)上的資料來(lái)看只能驚嘆其發(fā)展速度太快,讓我意外的是其作者是華人的前提下作品這么受歡迎。 網(wǎng)上的博客和教程各種組合。以至于我都有些感覺(jué)out。各種vue+webpack、vue+react、vue+es6+npm等等。琳瑯滿目。
開(kāi)篇主要是初次了解下vue.js,包括v-model、v-if、v-else、v-show、v-for(2.0對(duì)$index和$key舍棄,2.0要用index屬性語(yǔ)法為 v-for="(person,index) in persons")、v-on。
看圖

看代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue.js CURD</title>
<meta id="viewport" name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1">
<link rel="stylesheet" >
<script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>
<body>
<div class="row" id="app">
<div class="col-xs-12 col-md-8">
<fieldset>
<legend>New Person</legend>
<div class="form-group">
<label>ID</label>
<input type="text" v-model="newPerson.id"/>
</div>
<div class="form-group">
<label>Name:</label>
<input type="text" v-model="newPerson.name"/>
</div>
<div class="form-group">
<label>Age:</label>
<input type="text" v-model="newPerson.age"/>
</div>
<div class="form-group">
<label>Sex:</label>
<select v-model="newPerson.sex">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
<div class="form-group">
<label></label>
<button @click="createorupdate">ok</button>
</div>
</fieldset>
</div>
<div class="col-xs-12 col-md-8">
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>name</th>
<th>age</th>
<th>sex</th>
</tr>
</thead>
<tbody>
<tr v-for="(person,index) in persons">
<td>{{person.id}}</td>
<td>{{person.name}}</td>
<td>{{person.age}}</td>
<td>{{person.sex}}</td>
<td><button @click="deletePerson(index)">delete</button></td>
<td><button @click="update(index)">update</button></td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
Array.prototype.arrIndex=function(obj){
for(let i=0;i<this.length;i++){
var tmp=this[i];
if(tmp==obj){
return i;
}
}
}
var vm=new Vue({
el:'#app',
data:{
editLock:1,
newPerson:{
id:0,
name:'',
age:0,
sex:'male'
},
persons:[{
id:1,
name: 'Jack',
age: 30,
sex: 'Male'
}, {
id:2,
name: 'Bill',
age: 26,
sex: 'Male'
}, {
id:3,
name: 'Tracy',
age: 22,
sex: 'Female'
}, {
id:4,
name: 'Chris',
age: 36,
sex: 'Male'
}]
},
methods:{
create:function(){
this.persons.push(this.newPerson);
this.newPerson={id:0,name:'',age:0,sex:'male'};
},
createorupdate:function(){
if(this.editLock===1){
this.persons.push(this.newPerson);
}else{
//刪除老對(duì)象
var aindex=this.persons.arrIndex(this.newPerson);
console.log(aindex);
this.persons.splice(aindex,1);
//插入新對(duì)象
this.persons.push(this.newPerson);
}
this.newPerson={id:0,name:'',age:0,sex:'male'};
},
deletePerson:function(idx){
this.persons.splice(idx,1);
},
update:function(idx){
var p =this.persons[idx];
this.editLock=0;
this.newPerson=p;
}
}
})
</script>
</body>
</html>
參考資料:
https://cn.vuejs.org/v2/guide/routing.html
//www.dhdzp.com/article/98791.htm
//www.dhdzp.com/article/96426.htm
本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn)
- 前端框架Vue.js中Directive知識(shí)詳解
- 前端框架Vue.js構(gòu)建大型應(yīng)用淺析
- Vue2.0 UI框架ElementUI使用方法詳解
- vue.js中mint-ui框架的使用方法
- 前端框架學(xué)習(xí)總結(jié)之Angular、React與Vue的比較詳解
- Vue.js 2.0 和 React、Augular等其他前端框架大比拼
- 利用VUE框架,實(shí)現(xiàn)列表分頁(yè)功能示例代碼
- 基于Vuejs框架實(shí)現(xiàn)翻頁(yè)組件
- 前端主流框架vue學(xué)習(xí)筆記第二篇
相關(guān)文章
在vue項(xiàng)目中引入highcharts圖表的方法
今天小編就為大家分享一篇關(guān)于在vue項(xiàng)目中引入highcharts圖表的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
vue項(xiàng)目中定義全局變量、函數(shù)的幾種方法
這篇文章主要介紹了vue項(xiàng)目中定義全局變量、函數(shù)的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
vue實(shí)現(xiàn)簡(jiǎn)單圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue在頁(yè)面右上角實(shí)現(xiàn)可懸浮/隱藏的系統(tǒng)菜單
這篇文章主要介紹了Vue在頁(yè)面右上角實(shí)現(xiàn)可懸浮/隱藏的系統(tǒng)菜單,實(shí)現(xiàn)思路大概是通過(guò)props將showCancel這個(gè)Boolean值傳遞到子組件,對(duì)父子組件分別綁定事件,來(lái)控制這個(gè)系統(tǒng)菜單的顯示狀態(tài)。需要的朋友可以參考下2018-05-05
vue中使用iview自定義驗(yàn)證關(guān)鍵詞輸入框問(wèn)題及解決方法
這篇文章主要介紹了vue中使用iview自定義驗(yàn)證關(guān)鍵詞輸入框問(wèn)題及解決方法,本文通過(guò)實(shí)例結(jié)合代碼的形式給大家介紹解決方法,需要的朋友可以參考下2018-03-03

