vue实现图书管理demo

年后公司的项目要求用到vue.js知识,我angular没有学,node.js和react也只是了解了一点点,所以学起来比较困难。如果你想学vue.js的知识,推荐网址:http://vuejs.org/

详细内容如下:

一、图书管理demo用的知识点

1、bootstrap    http://getbootstrap.com/

2、vuejs    http://getbootstrap.com/

具体代码如下:

html部分

<div id="app" class="container">
            <table class="table table-bordered">
                <div v-for = "book in books">
                    <tr>
                        <th>书名</th>
                        <th>书的价格</th>
                        <th>书的数量</th>
                        <th>小计</th>
                        <th>操作</th>
                    </tr>
                    <tr v-for = "(index,book ) in books">
                        <td>{{book.name}}</td>
                        <td>{{book.price}}</td>
                        <td><button @click="book.count&&book.count--">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
                        <td>{{book.price*book.count}}</td>
                        <td><button class="btn btn-danger" @click="deleteBook(book)">删除</button></td>
                    </tr>
                    <tr>
                        <td colspan="5">
                            总价{{total}}
                        </td>
                    </tr>
                </div>
            </table>
            <div class="form-group">
                <label for="bookname" id="bookname">书名</label>
                <input type="text" v-model="list.name" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="bookprice" id="bookprice">价格</label>
                <input type="text" v-model="list.price" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="bookcount" id="bookcount">数量</label>
                <input type="text" v-model="list.count" class="form-control"/>
            </div>
            <div class="form-group">
                <button class="btn btn-primary" @click="add">添加</button>
            </div>
        </div>

脚本部分

<script src="js/vue.js"></script>
        <script>
            var vm = new Vue({
                el:"#app",
                data:{
                    books:[
                    {name:'VUE js',price:40,count:1},
                    {name:'NODE js',price:20,count:1},
                    {name:'REACT js',price:30,count:1},
                    {name:'ANGULAR js',price:100,count:1},
                    {name:'JQUERY js',price:50,count:1},
                    ],
                    list:{
                        name:'',
                        price:'',
                        count:''
                    }
                },
                methods:{
                    deleteBook(book){
                        // vue 给我们提供了一个 $remove的方法,在数组中删除
                        this.books.$remove(book);
                        /*this.books = this.books.filter((item)=>{
                            return item != book
                        })*/
                    },
                    add(){
                        this.books.push(this.list);
                        this.list = {
                            name:'',
                            price:'',
                            count:''
                        }
                    }
                },
                computed:{
                    total(){
                        var sum = 0;
                        this.books.forEach(item =>{
                            sum += item.price*item.count;
                        })
                        return sum;
                    }
                }
            });
        </script>

遇到难点总结

当数量小于0时,判断方法,解决方法有很多种,下面总结有3中方法

(一)最简单的方法

根据逻辑判断,这里要注意逻辑判断的顺序,代码如下:

<td><button @click="book.count&&book.count--">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>

(二)这里要注意this指向问题

<td><button @click="min(index,book.count)">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
methods:{
                    min(index){
                        if(this.books[index].count>0){
                             this.books[index].count = parseInt(this.books[index].count) - 1;
                        }
                    },
                    deleteBook(book){
                        // vue 给我们提供了一个 $remove的方法,在数组中删除
                        this.books.$remove(book);
                        /*this.books = this.books.filter((item)=>{
                            return item != book
                        })*/
                    },
                    add(){
                        this.books.push(this.list);
                        this.list = {
                            name:'',
                            price:'',
                            count:''
                        }
                    }
                }

(三) v-on执行时传参问题

<td><button @click="min(book)">-</button><input type="text" v-model="book.count" /><button @click="book.count++">+</button></td>
min(book){
	if(book.count>0){
		book.count = parseInt(book.count) - 1;
	}
}

总结:

v-on执行方法的时候需要传递参数的时候添加(),如果不需要传递参数就不用加上() 
如果需要传递参数,我们需要手动传入事件源

建议:

1、如果您有充足的时间来学习vue,务必要把js基础打好,学习下angular.js

2、学会在网上找资料。

作者:白开水

出处:http://www.cnblogs.com/hongxp/

本文以学习、总结和分享为主,如需转载,请联系本人,标明作者和出处,非商业用途!

 

原文地址:https://www.cnblogs.com/hongxp/p/6420716.html