UNIAPP不支持filters傳輸多個形參值 ,但可以支持傳輸對象
1 <template> 2 <view> 3 <view class="fix-list" v-for="(item, index) in mockList" :key="index"> 4 <view class="fix-list-item" @click="handlePhone"> 5 <view>手機號碼</view> 6 <view style="color: #409EFE;">{{ item | phoneVal }}</view> 7 </view> 8 </view> 9 </view> 10 </template> 11 12 <script> 13 export default { 14 data() { 15 return { 16 mockList: [{ mobile: '13111225554', tag: 1 }, { mobile: '13254565421', tag: 2 }] 17 }; 18 }, 19 filters: { 20 phoneVal(item) { 21 if (item.tag == 1) { 22 return hidePhone(item.mobile); 23 } else { 24 return item.mobile; 25 } 26 // 通過形參傳過來的對象(item)即可繼續走下面的邏輯 , 判斷標識tag:1即可顯示完整號碼,否則隱藏中間4位數 27 } 28 }, 29 methods: { 30 31 } 32 }; 33 34 35 </script> 36 37 <style lang="scss"> 38 39 </style> 40
1 const hidePhone = cellValue => { 2 if (Number(cellValue) && String(cellValue).length === 11) { 3 let mobile = String(cellValue); 4 let reg = /^(\d{3})\d{4}(\d{4})$/; 5 return mobile.replace(reg, '$1****$2'); 6 } else { 7 return cellValue; 8 } 9 };