欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用vue2實(shí)現(xiàn)購物車和地址選配功能

 更新時(shí)間:2018年03月29日 11:48:35   作者:起風(fēng)了  
這篇文章主要介紹了使用vue2實(shí)現(xiàn)購物車和地址選配功能,本文通過實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下

首先,vue基礎(chǔ)js寫法

new Vue({
  el:"#app",
  //模型
  data:{
  },
  filters:{
  },
  mounted:function(){
    this.$nextTick(function(){
    //初始化調(diào)用
    });
  },
  computed:{
    //實(shí)時(shí)計(jì)算
  },
  methods:{
  }
});

v-for

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

v-model

(實(shí)時(shí)更新)

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

v-bind

<a href="javascript:;" class="item-check-btn" v-bind:class="{'check':item.checked}">
<!--可通過更改item.checked的值設(shè)置是否選中-->
<!--必須用v-bind 不可直接在class里面直接使用{{}}-->
<!--v-bind:class= 可簡寫為 :class= -->

filters過濾器的使用

1.html引用方式

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

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; //保留兩位小數(shù) 結(jié)果eg:¥19.00元
});

調(diào)用methods中的方法:

@click="method(param)"
//或者
@click="delFlag=false"
@click="limitNum=addressList.length"

computed 實(shí)時(shí)計(jì)算

如下:默認(rèn)顯示三條數(shù)據(jù),點(diǎn)擊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>

data:{
    limitNum:3
  },
computed:{
  filterAddress:function(){
    return this.addressList.slice(0,this.limitNum);
  }
},

先提出一兩個(gè)經(jīng)典的實(shí)例

1.以下實(shí)現(xiàn)了對(duì)循環(huán)卡片的點(diǎn)擊 選中

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

2.以下實(shí)現(xiàn)了對(duì)固定卡片的點(diǎn)擊 選中

<ul>
  <li v-bind:class="{'check':shippingMethod==1}" @click="shippingMethod=1">
   <div class="name">標(biāo)準(zhǔn)配送</div>
   <div class="price">Free</div>
  </li >
  <li v-bind:class="{'check':shippingMethod==2}" @click="shippingMethod=2">
   <div class="name">高級(jí)配送</div>
   <div class="price">180</div>
  </li>
 </ul>
 <!--其中shippingMethod在js里需要定義-->

題外話:由于本人小白,學(xué)一點(diǎn)是一點(diǎn),額外記錄一下輔助彈出框 遮罩層的寫法

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

vue2的js語法 貼幾個(gè) 方便查用

1.調(diào)用后端方法

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循環(huán)

this.productList.forEach(function(item,index){
  if(typeof item.checked == 'undefined'){ 
  //如果item中沒有checked屬性 在item對(duì)象中添加checked屬性,值為true
    _this.$set(item,"checked",true);//局部注冊(cè)
    Vue.set(item,"checked",true);//全局注冊(cè)
  }
});

附上鏈接:碼云地址vue2_study

總結(jié)

以上所述是小編給大家介紹的使用vue2實(shí)現(xiàn)購物車和地址選配功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論