这次给大家带来vue2实现购物车与地址选配案例分析,vue2实现购物车与地址选配的注意事项有哪些,下面就是实战案例,一起来看一下。
首先,vue基础js写法
new Vue({ el:"#app", //模型 data:{ }, filters:{ }, mounted:function(){ this.$nextTick(function(){ //初始化调用 }); }, computed:{ //实时计算 }, methods:{ } });
v-for
{{item.productName}}
v-model
(实时更新)
<input><p>{{item.productQuantity}}</p>
v-bind
<a> <!--可通过更改item.checked的值设置是否选中--> <!--必须用v-bind 不可直接在class里面直接使用{{}}--> <!--v-bind:class= 可简写为 :class= --></a>
filters过滤器的使用
1.html引用方式
<p>{{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 显示所有
先提出一两个经典的实例
1.以下实现了对循环卡片的点击 选中
2.以下实现了对固定卡片的点击 选中
标准配送
Free
高级配送
180
题外话:由于本人小白,学一点是一点,额外记录一下辅助弹出框 遮罩层的写法
<p></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);//全局注册 } });
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是vue2实现购物车与地址选配案例分析的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号