【VUE】過濾器的使用,管道符號' | '


1.vue過濾器的使用,常用於多文本數據的格式化

<!-- 在雙花括號中 -->
{{ message | capitalize }}
{{ 數據 | 過濾器 }}
  • 過濾器函數接收表達式的值 (之前的操作鏈的結果) 作為第一個參數

  • 過濾器可以傳遞參數

{{ message | filter('type1', type2) }}

message將作為第一個參數傳遞給過濾器,'type1', type2,依次作為第二個 第三個參數
  • 過濾器可以串聯

{{ message | filterA | filterB }}

message的返回傳入filterA, 處理結果作為參數繼續傳入filterB 

2.vue實例

  • 金額的格式化:保留兩位0
 1  let formatMoney = (value, type) => {
 2     let moneyStr = '';
 3     if (isNan(Number(value))) {
 4         return false
 5     }
 6     if (value) {
 7       value = parseFloat(value).toFixed(2).split('.');
 8     } else {
 9       value = ['0', '00']
10     }
11     if (type === 'integer') {
12       moneyStr = '¥ ' + value[0];
13    } else if (type === 'float') {
14      moneyStr = value[1];
15    } else if (type === 'noIcon') {
16      moneyStr = value[0] + '.' + value[1];
17    } else {
18      moneyStr = '¥ ' + value[0] + '.' + value[1];
19    }
20    return moneyStr;
21  }
  • 數據處理,有數據取數據,沒有‘--‘展示
1 let filter = (value) => {
2   let flag;
3   flag = !value ? '--' : value
4   return flag
5 }
  • 從js文件中將兩個過濾器暴露出去
1 export default {
2   filter,
3   formatMoney
4 }
  • 在main.js中引入:
import filter from './filters/index';
  • 使用:

  獲取到數據unitmount,作為參數傳給filter ----> 有值就使用原來的值,沒有的話展示為空  ----->  處理后的數據繼續格式化,保留兩位0

1             <el-table-column
2               prop="unitamount"
3               min-width="150px"
4               label="保險金額(元)"
5               align="right">
6               <template slot-scope="scope">
7                 <span>{{ scope.row.unitamount | filter  | formatMoney('noIcon') }}</span>
8               </template>
9             </el-table-column>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM