年后公司的项目要求用到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、学会在网上找资料。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部