【问题标题】:$t is undefined inside of component filter$t 在组件过滤器内未定义
【发布时间】:2019-04-10 18:32:33
【问题描述】:

我的组件上有过滤器

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]
}
}

但我有一个错误 类型错误:无法读取未定义的属性“$t”

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

【问题讨论】:

  • filters: 里面这个 = undefined,不是你的 vue 实例。并且“过滤器应该是纯粹的 - 没有副作用,只有数据输入和数据输出”......您可以使用方法或计算属性。
  • 我明白了,所以它应该是独立的。谢谢!

标签: vue.js vuejs2 nuxt.js


【解决方案1】:

像这样重新整理您的代码。它会工作的

   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)}} 

【讨论】:

    猜你喜欢
    • 2019-11-27
    • 2021-07-10
    • 2022-01-08
    • 2022-08-17
    • 2019-03-20
    • 2022-10-24
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多