使用vue2實(shí)現(xiàn)購物車和地址選配功能
首先,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)站的支持!
- vue 2.0 購物車小球拋物線的示例代碼
- vuex實(shí)現(xiàn)的簡單購物車功能示例
- vue實(shí)現(xiàn)的仿淘寶購物車功能詳解
- vuejs手把手教你寫一個(gè)完整的購物車實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購物車功能
- vue實(shí)現(xiàn)商城購物車功能
- 用vue和node寫的簡易購物車實(shí)現(xiàn)
- Vue.js搭建移動(dòng)端購物車界面
- 用vuex寫了一個(gè)購物車H5頁面的示例代碼
- Vue實(shí)現(xiàn)購物車場(chǎng)景下的應(yīng)用
- vue實(shí)現(xiàn)購物車拋物線小球動(dòng)畫效果的方法詳解
相關(guān)文章
vue中Echarts使用動(dòng)態(tài)數(shù)據(jù)的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了vue中Echarts使用動(dòng)態(tài)數(shù)據(jù)的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue 中 beforeRouteEnter 死循環(huán)的問題
這篇文章主要介紹了vue beforeRouteEnter 死循環(huán)的問題,在文章末尾給大家補(bǔ)充介紹了vue中beforeRouteEnter使用的誤區(qū),需要的朋友可以參考下2019-04-04前端vue按1920*1080設(shè)計(jì)圖的頁面適配屏幕縮放并適配4K屏詳解
最近在做一個(gè)數(shù)據(jù)可視化的項(xiàng)目,整個(gè)項(xiàng)目全是大屏展示,期間也是遇到很多問題,最令人頭疼的就是大屏的適配,下面這篇文章主要給大家介紹了前端vue按1920*1080設(shè)計(jì)圖的頁面適配屏幕縮放并適配4K屏的相關(guān)資料,需要的朋友可以參考下2022-11-11Vue3組合式函數(shù)Composable實(shí)戰(zhàn)ref和unref使用
這篇文章主要為大家介紹了Vue3組合式函數(shù)Composable實(shí)戰(zhàn)ref和unref使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue?element-plus中el-input修改邊框border的方法
element樣式還是蠻好的,只是有時(shí)候我們需要做一些調(diào)整,比如el-input的邊框,下面這篇文章主要給大家介紹了關(guān)于vue?element-plus中el-input修改邊框border的相關(guān)資料,需要的朋友可以參考下2022-09-09Vue實(shí)現(xiàn)計(jì)數(shù)器案例
這篇文章主要為大家詳細(xì)介紹了Vue計(jì)數(shù)器案例的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06關(guān)于vue項(xiàng)目proxyTable配置和部署服務(wù)器的問題
這篇文章主要介紹了關(guān)于vue項(xiàng)目proxyTable配置和部署服務(wù)器的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue.js實(shí)現(xiàn)圖片的隨意拖動(dòng)方法
下面小編就為大家分享一篇Vue.js實(shí)現(xiàn)圖片的隨意拖動(dòng)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03