在Vuemethods中調(diào)用filters里的過濾器實例

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

專業(yè)領(lǐng)域包括做網(wǎng)站、網(wǎng)站設(shè)計、商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。

網(wǎng)友hongz1125提出的解決辦法:

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

下面是我的方法,有點復(fù)雜。建議使用上面網(wǎng)友說的方法。

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 = '優(yōu)秀';
      }
      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 = '優(yōu)秀';
    }
    return score
  },
  getPhysicalResult() {
    this.$http.get('/rc_ChildTest/testResult').then((res) => {
      this.isDisplayIcon = this.formatScore(score參數(shù));//使用過濾器方法,需要單獨寫一個一模一樣的方法。通過this調(diào)用
    })
  },
  },

解決方法:

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 = '優(yōu)秀';
    }
    return score
  },
}

2.導(dǎo)入report.js 并使用

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

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

以上這篇在Vue methods中調(diào)用filters里的過濾器實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。

標題名稱:在Vuemethods中調(diào)用filters里的過濾器實例
URL網(wǎng)址:http://muchs.cn/article16/gphodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、做網(wǎng)站、外貿(mào)建站、網(wǎng)站內(nèi)鏈、標簽優(yōu)化網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司