码迷,mamicode.com
首页 > Web开发 > 详细

vue.js之常操作(实例)

时间:2017-08-03 15:02:11      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:css   nbsp   logs   inline   charset   methods   doc   var   实例   

听说大家都开始用react、vue这些MVVM框架了,为了不落后,还是学学吧!(之前只对angular了解一点,时间一长,也忘得差不多了,所以学习vue相当于从小白开始)

vue.js官网看一下,先学习几个基本指令:v-if、v-show、v-bind、v-model、v-for、v-on,然后一起做一个练习。

技术分享

要实现上图功能,构思一下应该会用到什么指令,代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>vue综合练习</title>
        <link rel="stylesheet" href="css/bootstrap.css" />
        <script src="https://unpkg.com/vue"></script>
        <style>
            table th,table td{
                text-align: center;
            }
            
        </style>
    </head>
    <body >
        <div id="app">
            <div class="form form-horizontal col-lg-10 col-md-10 col-lg-offset-1 col-md-offset-1">
                <div class="form-group">
                    <label for="name" class="col-lg-1 col-md-1 col-sm-2 control-label">姓名:</label>
                    <div class=" col-lg-11 col-md-11 col-sm-10">
                        <input type="text" class="form-control" id="name" placeholder="请输入姓名" v-model="newPerson.name"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="age" class="col-lg-1 col-md-1 col-sm-2 control-label">年龄:</label>
                    <div class=" col-lg-11 col-md-11 col-sm-10">
                        <input type="text" class="form-control" id="age" placeholder="请输入年龄" v-model="newPerson.age"/>
                    </div>
                </div>
                <div class="form-group">
                    <label for="sex" class="col-lg-1 col-md-1  col-sm-2 control-label">性别:</label>
                    <div class=" col-lg-11 col-md-11 col-sm-10">
                        <label class="radio-inline"><input type="radio" name="radiolist" v-model="newPerson.sex" value="男"/></label>  
                        <label class="radio-inline"><input type="radio" name="radiolist" v-model="newPerson.sex" value="女"/></label>  
                       
                    </div>
                </div>
                
                <div class="form-group">
                    <button class="col-lg-1 col-md-1 col-sm-2 col-lg-offset-6 col-md-offset-6 btn btn-success" v-on:click="creat()">添加</button>
                </div>
            </div>
            <div class="col-lg-10 col-md-10 col-lg-offset-1 col-md-offset-1">
                <table class="table table-bordered table-striped table-hover" >
                    <thead>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>操作</th>
                    </thead>
                    <tbody>
                        <tr v-for="(person,index) in people">
                            <td>{{person.name}}</td>
                            <td>{{person.sex}}</td>
                            <td>{{person.age}}</td>
                            <td><button class="btn btn-success" v-on:click="deletePerson(index)">删除</button></td>
                        </tr>
                        
                    </tbody>
                </table>
                
            </div>
        </div>
        <script>
            var app=new Vue({
                el:"#app",
                data:{
                    newPerson:{
                        name:"",
                        sex:"",//第一次的默认值
                        age:"0",
                    },
                    people:[{
                        name:"jack",
                        sex:"",
                        age:28,
                    },
                    {
                        name:"Bill",
                        sex:"",
                        age:25,
                    },
                    {
                        name:"Tracy",
                        sex:"",
                        age:20,
                    },
                    ]
                },
                methods:{
                    creat:function(){
                        this.people.push(this.newPerson);
                        this.newPerson={name:"",sex:"",age:"0"}//sex每次添加时不选择的默认值
                    },
                    deletePerson:function(index){
                        this.people.splice(index,1);
                        
                    }
                },
                
            })
        </script>
        
    </body>
</html>

 

vue.js之常操作(实例)

标签:css   nbsp   logs   inline   charset   methods   doc   var   实例   

原文地址:http://www.cnblogs.com/wj19940520/p/7279349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!