前言:用到優惠券選擇組件,vant官網demo很不友好。百度了下,不少網友都是片段,都是做技術的,就不能貼個完整的么?給個demo讓別人一個勁兒的找bug完善。
這里就直接貼完整demo了,組件注冊全部局部注冊。
示例代碼:
1 <template> 2 <div> 3 <!-- 優惠券單元格 --> 4 <van-coupon-cell :coupons="coupons" :chosen-coupon="chosenCoupon" @click="showList = true" /> 5 6 <!-- 優惠券列表 --> 7 <van-popup v-model="showList" position="bottom"> 8 <van-coupon-list :coupons="coupons" :chosen-coupon="chosenCoupon" :disabled-coupons="disabledCoupons" @change="onChange" @exchange="onExchange" /> 9 </van-popup> 10 </div> 11 </template> 12 <script> 13 const coupon = { 14 id: '1', // 優惠券id 15 discount: 0, // 折扣券 折值 整數 為0不顯示折 16 denominations: 2000, // 優惠券金額 單位分 17 originCondition: 5000, // 滿減規則金額 單位分 為0顯示無門檻 18 value: 12, // 折扣券優惠金額,單位分 19 name: '新人大禮包', // 優惠券名稱 20 description: '喜歡你就用', // 描述信息 21 reason: '訂單未滿200元', // 不可用原因,優惠券不可用時展示 22 startAt: parseInt(1556337595530 / 1000), // 卡有效開始時間 (時間戳, 單位秒) 23 endAt: parseInt(1559337595530 / 1000) // 卡失效日期 (時間戳, 單位秒) 24 } 25 26 import { CouponCell, CouponList,Popup } from 'vant'; 27 export default { 28 components: { 29 [CouponCell.name]: CouponCell, 30 [CouponList.name]: CouponList, 31 [Popup.name]: Popup 32 }, 33 data() { 34 return { 35 showList:false, 36 chosenCoupon: -1, 37 coupons: [coupon], 38 disabledCoupons: [coupon] 39 }; 40 }, 41 42 methods: { 43 onChange(index) { 44 this.showList = false; 45 this.chosenCoupon = index; 46 }, 47 onExchange(code) { 48 this.coupons.push(coupon); 49 } 50 } 51 }; 52 </script>
注:其中的 coupon 可以改成pros傳過來,原來的數據可以在引用頁面或者組件中進行數據結構轉換,頁面直接引用就好了。