当数量小于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、学会在网上找资料。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持黑区网络。
