代码之家  ›  专栏  ›  技术社区  ›  LakiGeri

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

  •  0
  • LakiGeri  · 技术社区  · 7 年前

    我想从字典中设置一个默认属性,如下所示:

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

    这个 $t 是一个 vue-i18n ,如果未在父类中定义标题,则我希望从字典中设置标题。但我错了:

    未知类型错误:this.$t不是函数

    如果我重新加载时没有 this 是的。

    未捕获引用错误:$t未定义

    但是,如果我在mount方法中注销这个值,它就会工作得很好。

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

    提前谢谢!

    1 回复  |  直到 7 年前
        1
  •  7
  •   artoju    7 年前

    一种解决方案是将密钥或部分密钥作为默认道具,如

    title: {
       type: String,
       default: "basic.confirm", //or "confirm"
     },
    

    在模板中:

    <h1>{{ $t(title) }}</h1> //or $t("basic." + title)
    

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

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