如何翻译道具的默认值?

How can traslate the default value of the props?

我想翻译文本,我有这个代码:

title: {
  type: String,
  default: function () {
    return this.$t("basic.confirm")
  }
},

我收到这个错误:

vue.common.dev.js?4650:630 [Vue warn]: nextTick 出错: "TypeError: Cannot read 属性 '$t' of undefined"

但是当我在模板中使用时,效果很好:

{{$t("basic.confirm")}}

在创建组件实例之前验证道具。因此,在您尝试使用“this”关键字时不可用,因为“this”尚不存在。

不过,您可以做的是创建一个计算变量,它实际转换 prop,然后在您的模板或任何您想要的地方使用它。这是伪代码:

computed: {
      computedTitle: function () {
            // Prop fallback value.
            if (!this.title) {
                return this.$t('basic.confirm');
            }
            // Actual prop value.
            return this.title;
        }
}