使用vue2实现购物车和地址选配功能

感谢免费教学:https://www.imooc.com/video/14039

首先,vue基础js写法

new Vue({
    el:"#app",
    //模型
    data:{
    },
    filters:{
    },
    mounted:function(){
        this.$nextTick(function(){
        //初始化调用
        });
    },
    computed:{
        //实时计算
    },
    methods:{
    }
});
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

v-for

<li v-for="(item,index) in productList">
    <div class="item-name">{{item.productName}}</div>
</li>
   
   
  • 1
  • 2
  • 3

v-model

(实时更新)

<input type="text" value="0" disabled v-model="item.productQuantity">
<div class="item-price-total">{{item.productQuantity}}</div>
   
   
  • 1
  • 2

v-bind

<a href="javascript:;" class="item-check-btn" v-bind:class="{'check':item.checked}">
<!--可通过更改item.checked的值设置是否选中-->
<!--必须用v-bind 不可直接在class里面直接使用{{}}-->
<!--v-bind:class= 可简写为 :class= -->
   
   
  • 1
  • 2
  • 3
  • 4

filters过滤器的使用

1.html引用方式

<div class="item-price">{{item.productPrice | money('元')}}</div>
   
   
  • 1

2.过滤器

filters:{
    formatMoney:function(value,type){
        return "¥"+value.toFixed(2)+ type;
    }
},
   
   
  • 1
  • 2
  • 3
  • 4
  • 5

3.全局过滤器(写在new Vue的外面)

Vue.filter("money",function(value,type){
    return "¥"+value.toFixed(2) + type;  //保留两位小数 结果eg:¥19.00元
});
   
   
  • 1
  • 2
  • 3

调用methods中的方法:

@click="method(param)"
//或者
@click="delFlag=false"
@click="limitNum=addressList.length"
   
   
  • 1
  • 2
  • 3
  • 4

computed 实时计算

如下:默认显示三条数据,点击more 显示所有

<li v-for="(item,index) in filterAddress">

<div 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>
</div>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
data:{
        limitNum:3 },
computed:{
    filterAddress:function(){ return this.addressList.slice(0,this.limitNum);
    }
},
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

先提出一两个经典的实例

1.以下实现了对循环卡片的点击 选中

<li v-for="(item,index) in filterAddress" v-bind:class="{'check':index==currentIndex}" 
@click="currentIndex=index">
<!--其中currentIndex在js里需要定义-->
   
   
  • 1
  • 2
  • 3

2.以下实现了对固定卡片的点击 选中

<ul>
   <li v-bind:class="{'check':shippingMethod==1}" @click="shippingMethod=1">
     <div class="name">标准配送</div>
     <div class="price">Free</div>
   </li >
   <li v-bind:class="{'check':shippingMethod==2}" @click="shippingMethod=2">
     <div class="name">高级配送</div>
     <div class="price">180</div>
   </li>
 </ul>
 <!--其中shippingMethod在js里需要定义-->
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

题外话:由于本人小白,学一点是一点,额外记录一下辅助弹出框 遮罩层的写法

<div class="md-overlay" v-if="delFlag"></div>
   
   
  • 1

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;
});
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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);//全局注册
    }
});
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

附上链接:码云地址vue2_study

欢迎改错更正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值