国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

在Vue methods中調用filters里的過濾器實例

2024-05-06 15:30:58
字體:
來源:轉載
供稿:網友

需求:vue中,除了在模板中使用過濾器,有時候,methods中也需要使用filters中的過濾器,

網友hongz1125提出的解決辦法:

this.$options.filters[filter](...args)  //這種方法很簡單,也很實用

下面是我的方法,有點復雜。建議使用上面網友說的方法。

filters: {    formatScore(score) {      if (score < 20) {        score = '不合格';      } else if (score >= 20 && score <= 27) {        score = '合格';      } else if (score >= 28 && score <= 31) {        score = '良好';      } else if (score > 31) {        score = '優秀';      }      return score    }  },methods: {  formatScore(score) {    if (score < 20) {      score = '不合格';    } else if (score >= 20 && score <= 27) {      score = '合格';    } else if (score >= 28 && score <= 31) {      score = '良好';    } else if (score > 31) {      score = '優秀';    }    return score  },  getPhysicalResult() {    this.$http.get('/rc_ChildTest/testResult').then((res) => {      this.isDisplayIcon = this.formatScore(score參數);//使用過濾器方法,需要單獨寫一個一模一樣的方法。通過this調用    })  },  },

解決方法:

1.建立一個公共的report.js文件,提取封裝公共js

export default {  formatScore(score) {    if (score < 20) {      score = '不合格';    } else if (score >= 20 && score <= 27) {      score = '合格';    } else if (score >= 28 && score <= 31) {      score = '良好';    } else if (score > 31) {      score = '優秀';    }    return score  },}

2.導入report.js 并使用

import report from 'js文件所在位置'
filters: {    formatScore(score) {      return report.formatScore(score);//使用導入的report.js中的report.formatScore方法    }  },methods: {    getPhysicalResult() {      this.$http.get('/rc_ChildTest/testResult').then((res) => {        this.isDisplayIcon = report.formatScore(score參數);//這里直接使用導入的report.js中的report.formatScore方法。在methods方法中可以使用過濾器中的方法。      })    },  },

我是這么解決的,大家如果還有更好的方法,希望大家寫在評論里,歡迎大家批評指正。

以上這篇在Vue methods中調用filters里的過濾器實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 福建省| 大渡口区| 陵水| 大理市| 资溪县| 宿州市| 涡阳县| 始兴县| 东阿县| 公安县| 邛崃市| 石阡县| 容城县| 榕江县| 安远县| 卢氏县| 定远县| 太谷县| 房山区| 雷州市| 山西省| 抚远县| 塔城市| 临清市| 汉中市| 嵊泗县| 沙洋县| 丽江市| 黑龙江省| 井研县| 色达县| 金昌市| 三亚市| 大冶市| 淅川县| 博乐市| 曲麻莱县| 凤城市| 阿图什市| 鹤壁市| 会泽县|