≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

vue实现图书管理demo分析

人气:368 时间:2019-04-07

这篇文章主要为大家详细介绍了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>

 

遇到难点总结

 

【q1010.com温馨提示:图片暂缺】

当数量小于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.q1010.com/184/3665-0.html

注:关于vue实现图书管理demo分析的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • 浅谈vue路径优化之resolve
  • 什么是Vue.js框架 为什么选择它?(第一课)
  • vue中axios处理http发送请求的示例(Post和get)
  • vue router自动判断左右翻页转场动画效果
  • vue2.x select2 指令封装分析
  • vue router demo分析
  • Vue2.0父子组件传递函数的教程分析
  • vue-router实现tab标签页(单页面)分析
  • 如何抽象一个Vue公共组件
  • Vue组件之Tooltip的示例代码
上一篇:vue router仿天猫底部导航栏功能
下一篇:Vue组件之Tooltip的示例代码
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。