大纲:
Vue的基本模块语法
重点:指令,计算属性,监听属性
vue购物车实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style type="text/css">
table{width: 1200px;}
table th{width: 100px}
table td{width: 200px;text-align: center;}
a{text-decoration:none;color: black}
span{font-size: 20px;margin: 10px 10px}
strong{border: 1px solid;}
.checkPro{
background-color: gray;
}
.leftConent {
float: left;
}
.rightConent{
float: right;
}
</style>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<body>
<div id="test">
<table id="mytable">
<tr>
<th><input type="checkBox" @click="selectProduct(isSelectAll)" v-bind:checked="isSelectAll" />全选</th>
<th>商品</th>
<th>数量</th>
<th>单价(元)</th>
<th>金额(元)</th>
<th>操作</th>
</tr>
<tr v-for="(item,index) in productList">
<td><input type="checkBox" v-bind:checked="item.isSelect" @click="item.isSelect=!item.isSelect"/></td>
<td>{{item.proName}}</td>
<td><span><a href="#" @click="item.proNum>0?item.proNum--:''">-</a></span><strong>{{item.proNum}}</strong><span><a href="#" @click="item.proNum++">+</a></span></td>
<td>{{item.proPrice}}</td>
<td>{{item.proPrice*item.proNum}}</td>
<td><a href="#" @click="deletePro(index)">删除</a></td>
</tr>
</table>
<div class="checkPro">
<div class="leftConent">
<span><a href="#" @click="deleteProduct">删除所选产品</a></span>
</div>
<div class="rightConent">
<span>{{getTotal.totalNum}}件商品总计:¥{{getTotal.totalPrice}}</span>
</div>
</div>
</div>
</body>
<script >
new Vue({
el : "#test",
data : {
productList:[
{
'proName' :'剃须刀',
'proNum' : 2,
'proPrice' :1000,
},
{
'proName' :'小米耳机',
'proNum' : 10,
'proPrice' :100,
},
{
'proName' :'小米鼠标',
'proNum' : 5,
'proPrice' :100,
}
],
},
methods:{
selectProduct:function(_isSelect){
//遍历productList,全部取反
for (var i = 0, len = this.productList.length; i < len; i++) {
this.productList[i].isSelect = !_isSelect;
}
},
deletePro : function(index){
alert("你正在删除"+this.productList[index].proName);
this.productList.splice(index,1);
},
//删除已经选中(isSelect=true)的产品
deleteProduct:function () {
this.productList=this.productList.filter(function (item) {return !item.isSelect})
},
},
computed:{
//检测是否全选
isSelectAll:function(){
//如果productList中每一条数据的isSelect都为true,返回true,否则返回false;
return this.productList.every(function (val) { return val.isSelect});
},
getTotal:function(){
var prolist = this.productList.filter(function (val) { return val.isSelect}),
totalPri = 0;
for (var i = 0,len = prolist.length; i < len; i++) {
totalPri+=prolist[i].proPrice*prolist[i].proNum;
}
return {totalNum:prolist.length,totalPrice:totalPri}
},
},
mounted:function () {
var _this=this;
//为productList添加select(是否选中)字段,初始值为true
this.productList.map(function (item) {
_this.$set(item, 'isSelect', true);
})
}
})
</script>
</html>
< td><input type=“checkBox” v-bind:checked=“item.isSelect” @click=“item.isSelect=!item.isSelect”/>
js splice数组拼接函数
/**
* 1.往索引的后面添加元素
* 2.超过或者小于索引会在数组最后面或者最前面添加元素
* 3.打印数组元素最好使用for(var i = 0; i<arr.length; i++),因为for in 方法会打印用户自定义数组的方法
*/
var array = ["one", "two", "four"];
// splice(position, numberOfItemsToRemove, item...)
// 拼接函数(索引位置, 要删除元素的数量, 要增加的元素)
array.splice(2, 0, "three");
array.splice(4, 0, "five","six");
array.splice(100, 0, "seven");
array.splice(0, 0, "zero");
array.splice(-100, 0, "minus one");
for (var i in array) {
console.log(i + "="+array[i]);
}
Vue生命周期中mounted和created的区别
转载https://www.jianshu.com/p/f99d3e3d2256