Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的。相比于Angular.js,Vue.js提供了更加简洁、更易于理解的API,使得我们能够快速地上手并使用Vue.js。
先来看看实现的效果:
下面的例子会用到bootstrap.min.css以及vue.js,都可以从网上下载(文末有完整源码下载提供)。
实例 源码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue.js小demo</title> <link href="https://www.jb51.net/css/bootstrap.min.css"> </head> <body> <!-- 这是我们的view --> <div> <div > <div> <div> <label>Name:</label> <div> <input type="text" v-model="newPerson.name"/> </div> </div> <div> <label>Age:</label> <div> <input type="text" v-model="newPerson.age"> </div> </div> <div> <label>Sex:</label> <div> <select v-model="newPerson.sex"> <option value="Male">Male</option> <option value="Female">Female</option> </select> </div> </div> <div> <label for=""></label> <button @click="createPerson">Create</button> </div> </div> <div> <table> <thead> <tr > <th>Name</th> <th>Age</th> <th>Sex</th> <th>Delete</th> </tr> </thead> <tbody> <tr v-for="person in people"> <td>{{ person.name }}</td> <td>{{ person.age }}</td> <td>{{ person.sex }}</td> <td><button @click="deletePerson($index)">Delete</button></td> </tr> </tbody> </table> </div> </div> </div> </body> <script src="https://www.jb51.net/js/vue.js"></script> <script> //创建一个Vue实例或"ViewModel",它连接view与model var vm = new Vue({ el: '#app', data: { newPerson: { name: '', age: 0, sex: 'Male' }, people: [{ name: 'Jack', age: 30, sex: 'Male' }, { name: 'Bill', age: 26, sex: 'Male' }, { name: 'Tracy', age: 22, sex: 'Female' }, { name: 'Chris', age: 36, sex: 'Male' }] }, methods:{ createPerson: function(){ this.people.push(this.newPerson); // 添加完newPerson对象后,重置newPerson对象 this.newPerson = {name: '', age: 0, sex: 'Male'} }, deletePerson: function(index){ // 删一个数组元素 this.people.splice(index,1); } } }) </script> </html>
下载地址请点击 这里