从vue-i18n设置的vuejs默认属性

Lak*_*eri 3 javascript vue.js vue-i18n

我想从字典这样设置默认属性:

  props: {
    title: {
      type: String,
      default: this.$t("basic.confirm"),
    },
    description: {
      type: String,
    }
  }, ...
Run Code Online (Sandbox Code Playgroud)

$tvue-i18n,如果未在父类中定义标题,我想从字典中设置标题。但是我得到了错误:

未捕获的TypeError:this。$ t不是函数

类似的错误,如果我没有加载this

未捕获的ReferenceError:未定义$ t

但是,如果我在mount方法中注销此值,则效果很好。

有没有从字典设置默认属性的解决方案?

提前致谢!

art*_*oju 7

一种解决方案是将键或键的一部分作为默认道具,例如

title: {
   type: String,
   default: "basic.confirm", //or "confirm"
 },
Run Code Online (Sandbox Code Playgroud)

并在模板中:

<h1>{{ $t(title) }}</h1> //or $t("basic." + title)
Run Code Online (Sandbox Code Playgroud)

编辑:您可以访问$ t内部函数

title: {
  type: String,
  default: function () {
    return this.$t("basic.confirm")
  }
},
Run Code Online (Sandbox Code Playgroud)

  • 请注意,箭头语法无效:`default:()=&gt; {this。$ t(“ basic.confirm”)}`,但上述函数语法却无效。 (4认同)