使用vue2實現(xiàn)購物車和地址選配功能
首先,vue基礎(chǔ)js寫法
new Vue({ el:"#app", //模型 data:{ }, filters:{ }, mounted:function(){ this.$nextTick(function(){ //初始化調(diào)用 }); }, computed:{ //實時計算 }, methods:{ } });
v-for
<li v-for="(item,index) in productList"> <div class="item-name">{{item.productName}}</div> </li>
v-model
(實時更新)
<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 實時計算
如下:默認(rèn)顯示三條數(shù)據(jù),點擊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); } },
先提出一兩個經(jīng)典的實例
1.以下實現(xiàn)了對循環(huán)卡片的點擊 選中
<li v-for="(item,index) in filterAddress" v-bind:class="{'check':index==currentIndex}" @click="currentIndex=index"> <!--其中currentIndex在js里需要定義-->
2.以下實現(xià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">高級配送</div> <div class="price">180</div> </li> </ul> <!--其中shippingMethod在js里需要定義-->
題外話:由于本人小白,學(xué)一點是一點,額外記錄一下輔助彈出框 遮罩層的寫法
<div class="md-overlay" v-if="delFlag"></div>
vue2的js語法 貼幾個 方便查用
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對象中添加checked屬性,值為true _this.$set(item,"checked",true);//局部注冊 Vue.set(item,"checked",true);//全局注冊 } });
附上鏈接:碼云地址vue2_study
總結(jié)
以上所述是小編給大家介紹的使用vue2實現(xiàn)購物車和地址選配功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue中Echarts使用動態(tài)數(shù)據(jù)的兩種實現(xiàn)方式
這篇文章主要介紹了vue中Echarts使用動態(tài)數(shù)據(jù)的兩種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue 中 beforeRouteEnter 死循環(huán)的問題
這篇文章主要介紹了vue beforeRouteEnter 死循環(huán)的問題,在文章末尾給大家補充介紹了vue中beforeRouteEnter使用的誤區(qū),需要的朋友可以參考下2019-04-04前端vue按1920*1080設(shè)計圖的頁面適配屏幕縮放并適配4K屏詳解
最近在做一個數(shù)據(jù)可視化的項目,整個項目全是大屏展示,期間也是遇到很多問題,最令人頭疼的就是大屏的適配,下面這篇文章主要給大家介紹了前端vue按1920*1080設(shè)計圖的頁面適配屏幕縮放并適配4K屏的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue3組合式函數(shù)Composable實戰(zhàn)ref和unref使用
這篇文章主要為大家介紹了Vue3組合式函數(shù)Composable實戰(zhàn)ref和unref使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue?element-plus中el-input修改邊框border的方法
element樣式還是蠻好的,只是有時候我們需要做一些調(diào)整,比如el-input的邊框,下面這篇文章主要給大家介紹了關(guān)于vue?element-plus中el-input修改邊框border的相關(guān)資料,需要的朋友可以參考下2022-09-09關(guān)于vue項目proxyTable配置和部署服務(wù)器的問題
這篇文章主要介紹了關(guān)于vue項目proxyTable配置和部署服務(wù)器的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04