欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學(xué)堂HTML5培訓(xùn)之vue.js購(gòu)物車添加商品組件方法

2019-09-17 14:51:59 4529瀏覽

今天千鋒扣丁學(xué)堂HTML5培訓(xùn)老師給解答分享一篇關(guān)于vue.js購(gòu)物車添加商品組件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),下面我們一起來(lái)看一下吧。



現(xiàn)實(shí)向購(gòu)物車添加商品組件

代碼

<template>
<div class="cartcontrol">
 <!--商品減一區(qū)域-->
 <div class="reduce" v-show="food.count>0">
  <i class="icon-remove_circle_outline"></i>
 </div>
 <!--商品數(shù)量區(qū)域-->
 <div class="num" v-show="food.count>0">4</div>
 <!--商品加一區(qū)域-->
 <div class="add" @click="addCart">
  <i class="icon-add_circle"></i>
 </div>
</div>
</template>

<script>
export default {
  name: "Cartcontrol",
  props:{
    food:{
      type:Object
    }
  },
  methods:{
    //添加購(gòu)物車商品數(shù)量
    addCart(ele){
      if(!ele._constructed){
        //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點(diǎn)擊事件的event有個(gè)
        // 屬性區(qū)別_constructed,pc端瀏覽器的點(diǎn)擊事件的event中是沒有這個(gè)屬性的
        return;
      }
      //一開始food中是沒有商品數(shù)量count
      if(!this.food.count){
        // this.food.count = 1;count不是food對(duì)象中的屬性,直接這樣寫,在dom渲染的時(shí)候是無(wú)法感應(yīng)到count的變化
        this.$set(this.food,'count',1);
      }else{
        this.food.count++;
      }
      console.log(this.food.count);
    }
  }
}
</script>

<style scoped lang="stylus">
 
.cartcontrol
 
display flex
height .48rem
align-items center
.num
  font-size.2rem
  width .48rem
  text-align center
  color rgb(147,153,159)
.reduce,.add
  font-size .4rem
  color rgb(0,160,220)
</style>

對(duì)象中添加新的屬性,如果更新此屬性的值,是不會(huì)更新視圖的

addCart(ele){
if(!ele._constructed){
        //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點(diǎn)擊事件的event有個(gè)
        // 屬性區(qū)別_constructed,pc端瀏覽器的點(diǎn)擊事件的event中是沒有這個(gè)屬性的
        return;
      }
      //一開始food中是沒有商品數(shù)量count
      if(!this.food.count){
        this.food.count = 1;count不是food對(duì)象中的屬性,直接向food添加新屬性count,
        // 當(dāng)count值發(fā)生變化的時(shí)候在dom渲染的時(shí)候是無(wú)法感應(yīng)到count的變化
      }else{
        this.food.count++;
      }
      console.log(this.food.count);
    }

解決方法:使用$set可以觸發(fā)更新視圖,這樣當(dāng)count發(fā)生變化的時(shí)候,$set去觸發(fā)更新視圖addCart(ele){

if(!ele._constructed){
        //better-scroll的派發(fā)事件scroll的event和pc端瀏覽器的點(diǎn)擊事件的event有個(gè)
        // 屬性區(qū)別_constructed,pc端瀏覽器的點(diǎn)擊事件的event中是沒有這個(gè)屬性的
        return;
      }
      //一開始food中是沒有商品數(shù)量count
      if(!this.food.count){
        // this.food.count = 1;count不是food對(duì)象中的屬性,直接向food添加新屬性count,
        // 當(dāng)count值發(fā)生變化的時(shí)候在dom渲染的時(shí)候是無(wú)法感應(yīng)到count的變化
        this.$set(this.food,'count',1);
      }else{
        this.food.count++;
      }
      console.log(this.food.count);
    }

以上所述是小編給大家介紹的vue.js購(gòu)物車添加商品組件的實(shí)例代碼,希望對(duì)大家有所幫助,想要了解更多內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)咨詢。想要學(xué)好HTML5開發(fā)小編給大家推薦口碑良好的扣丁學(xué)堂,扣丁學(xué)堂有專業(yè)老師制定的HTML5學(xué)習(xí)路線圖輔助學(xué)員學(xué)習(xí),此外還有與時(shí)俱進(jìn)的HTML5課程體系和HTML5視頻教程供大家學(xué)習(xí),想要學(xué)好HTML5開發(fā)技術(shù)的小伙伴快快行動(dòng)吧??鄱W(xué)堂H5技術(shù)交流群:673883249。


                           【掃碼進(jìn)入HTML5VIP免費(fèi)公開課】  


     【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】        【掃碼進(jìn)入HTML5前端開發(fā)VIP免費(fèi)公開課】  



查看更多關(guān)于“HTML5開發(fā)技術(shù)資訊”的相關(guān)文章>>


標(biāo)簽: HTML5培訓(xùn) HTML5視頻教程 HTML5學(xué)習(xí)視頻 HTML5在線視頻 HTML5培訓(xùn)班 微信小程序

熱門專區(qū)

暫無(wú)熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部