標籤:ops pre splice text button 封裝 [] angularjs lin
1.VUE 前端簡單介紹
VUE JS是一個簡潔的雙向資料繫結架構,他的效能超過ANGULARJS,原因是實現的機制和ANGULARJS 不同,他在初始化時對資料增加了get和set方法,在資料set時,在資料屬性上添加監控,這樣資料發生改變時,就會觸發他上面的watcher,而ANGULARJS 是使用髒資料檢查來實現的。
另外VUEJS 入門比ANGULARJS 簡單,中文文檔也很齊全。
2.組件實現
在使用vue開發過程中,我們會需要擴充一些組件,在表單中使用,比如一個使用者選取器。
在VUEJS 封裝時,可以使用組件和指令。
在VUEJS中有V-MODEL 這個感覺和ANGULARJS 類似,實際完全不同,沒有ANGULARJS 複雜,他沒有象ANGULARJS的 ng-model 的viewtomodel和modeltoview特性,而且這個v-model 只能在input checkbox select 等控制項上進行使用,而 angularjs 可以 擴充 ngmodel實現他的render方法。。
另外我在使用 VUE指令時,實現雙向繫結,這個我研究了自訂指定的寫法,可能還是不太熟悉的原因,還沒有實現。
我改用組件來實現:
Vue.component(‘inputtext‘, { props: { ‘input‘:{ required: true },pname: { required: true }}, template: ‘<div><input type="text" v-model.lazy="input[pname]"><button @click="init" >初始化</button></div>‘, methods: { init:function () { this.input[this.pname]="測試"; } }})
在vue實現組件時,他使用的是單向資料流,在這裡我們使用 對象來實現雙向繫結。
在上面的代碼中,有兩個屬性 :
input,pname 其中input 是一個資料對象執行個體,pname: 只是一個字串。
使用方法:
<table border="1" width="400"> <tr> <td>name</td> <td> <inputtext :input="person" pname="name"></inputtext> </td> </tr> <tr> <td>age</td> <td> <input v-model="person.age"> </td> </tr> </table> <table border="1" width="400"> <tr> <td colspan="3"> <a href="#" @click="addRow(‘items‘)" class="btn btn-primary">添加</a> </td> </tr> <tr v-for="(item, index) in person.items"> <td > <inputtext :input="item" pname="school"></inputtext> </td> <td > <inputtext :input="item" pname="year"></inputtext> </td> <td > <a @click="removeRow(‘items‘,index)" >刪除</a> </td> </tr> </table>
<inputtext :input="item" pname="school"></inputtext>
<inputtext :input="person" pname="name"></inputtext>
組件使用代碼,這裡綁定了 item,person 資料,pname 為綁定欄位。
JS實現代碼:
var app8 = new Vue({ data:{ person:{name:"",age:0, items:[] } } , methods: { addRow: function (name) { this.person[name].push({school:"",year:""}) }, removeRow:function(name,i){ this.person[name].splice(i,1) ; } } }) app8.$mount(‘#app8‘)
這裡我們實現了 子表的資料添加和刪除。
介面效果:
VUE JS 使用組件實現雙向繫結