这次给大家带来如何操作vue2实现购物车和地址选配,操作vue2实现购物车和地址选配的注意事项有哪些,下面就是实战案例,一起来看一下。
首先,vue基础js写法
new vue({
el:#app,
//模型
data:{
},
filters:{
},
mounted:function(){
this.$nexttick(function(){
//初始化调用
});
},
computed:{
//实时计算
},
methods:{
}
});
v-for
<li v-for="(item,index) in productlist">
<p class="item-name">{{item.productname}}</p>
</li>
v-model
(实时更新)
<input type="text" value="0" disabled v-model="item.productquantity">
<p class="item-price-total">{{item.productquantity}}</p>
v-bind
<a href="javascript:;" class="item-check-btn" v-bind:class="{'check':item.checked}">
<!--可通过更改item.checked的值设置是否选中-->
<!--必须用v-bind 不可直接在class里面直接使用{{}}-->
<!--v-bind:class= 可简写为 :class= -->
filters过滤器的使用
1.html引用方式
<p class="item-price">{{item.productprice | money('元')}}</p>
2.过滤器
filters:{
formatmoney:function(value,type){
return ¥+value.tofixed(2)+ type;
}
},
3.全局过滤器(写在new vue的外面)
vue.filter(money,function(value,type){
return ¥+value.tofixed(2) + type; //保留两位小数 结果eg:¥19.00元
});
调用methods中的方法:
@click=method(param)
//或者
@click=delflag=false
@click=limitnum=addresslist.length
computed 实时计算
如下:默认显示三条数据,点击more 显示所有
<li v-for="(item,index) in filteraddress">
<p class="shipping-addr-more">
<a class="addr-more-btn up-down-btn" href="javascript:" @click="limitnum=addresslist.length">
more
<i class="i-up-down">
<i class="i-up-down-l"></i>
<i class="i-up-down-r"></i>
</i>
</a>
</p>
data:{
limitnum:3
},
computed:{
filteraddress:function(){
return this.addresslist.slice(0,this.limitnum);
}
},
先提出一两个经典的实例
1.以下实现了对循环卡片的点击 选中
<li v-for="(item,index) in filteraddress" v-bind:class="{'check':index==currentindex}"
@click="currentindex=index">
<!--其中currentindex在js里需要定义-->
2.以下实现了对固定卡片的点击 选中
<ul>
<li v-bind:class="{'check':shippingmethod==1}" @click="shippingmethod=1">
<p class="name">标准配送</p>
<p class="price">free</p>
</li >
<li v-bind:class="{'check':shippingmethod==2}" @click="shippingmethod=2">
<p class="name">高级配送</p>
<p class="price">180</p>
</li>
</ul>
<!--其中shippingmethod在js里需要定义-->
题外话:由于本人小白,学一点是一点,额外记录一下辅助弹出框 遮罩层的写法
<p class="md-overlay" v-if="delflag"></p>
vue2的js语法 贴几个 方便查用
1.调用后端方法
var _this = this;
this.$http.get(data/address.json).then(function(response){
_this.addresslist = response; //这里不能直接用this 此this非彼this 所以只能声明_this
});
//以下为es6写法,就可以直接用this了
let _this = this; //没用,就放这看看~
this.$http.get(data/cartdata.json,{id:123}).then(res=>{
this.productlist = res.data.result.list;
});
2.foreach循环
this.productlist.foreach(function(item,index){
if(typeof item.checked == 'undefined'){
//如果item中没有checked属性 在item对象中添加checked属性,值为true
_this.$set(item,checked,true);//局部注册
vue.set(item,checked,true);//全局注册
}
});
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
怎样使用angular对服务端进行渲染
怎样使用vue实现全选反选
以上就是如何操作vue2实现购物车和地址选配的详细内容。