[$ t在组件过滤器内部未定义

问题描述 投票:0回答:1

我的组件上有过滤器

filters: {
formatStatus (value) {
  let status = {
    active: {
      text: this.$t('general.successful'),
      class: 'secondary--text'
    },
    processing: {
      text: this.$t('general.processing'),
      class: 'tertiary--text'
    },
    waiting: {
      text: this.$t('general.pending'),
      class: 'tertiary--text'
    }
  }
  return status[value]
}
}

但是我遇到一个错误TypeError:无法读取未定义的属性'$ t'

但在方法上,$ t工作正常。

vue.js vuejs2 nuxt
1个回答
0
投票

这样散布您的代码。它会工作

   filters: {
formatStatus (value,self) {
  let status = {
    active: {
      text: self.$t('general.successful'),
      class: 'secondary--text'
    },
    processing: {
      text: self.$t('general.processing'),
      class: 'tertiary--text'
    },
    waiting: {
      text: self.$t('general.pending'),
      class: 'tertiary--text'
    }
  }
  return status[value]
}
}

像这样调用过滤器:

{{yourvalue | formatStatus(this)}} 
© www.soinside.com 2019 - 2024. All rights reserved.