【问题标题】:Vue Mixin computed function pass parametersVue Mixin 计算函数传参
【发布时间】:2018-05-11 05:13:02
【问题描述】:

所以我不想像 vue i18next 库使用 $t 那样使用我的 mixin

<div>
      <strong>{{ $t("loadbundle", {lang: this.lang}) }}</strong>
</div>

我可以在源代码中知道它是 mixin 中的计算属性

  Vue.mixin({
    computed: {
      $t() {
        const getKey = getByKey(this._i18nOptions, this.$i18n ? this.$i18n.i18next.options : {});

        if (this._i18nOptions && this._i18nOptions.namespaces) {
          const { lng, namespaces } = this._i18nOptions;

          const fixedT = this.$i18n.i18next.getFixedT(lng, namespaces);
          return (key, options) => fixedT(getKey(key), options, this.$i18n.i18nLoadedAt);
        }

        return (key, options) =>
          this.$i18n.i18next.t(getKey(key), options, this.$i18n.i18nLoadedAt);
      },
    }

但我不知道他是如何让它像函数一样调用的,以及他是如何获取传入的参数的。

我不能使用方法,因为每次更新时它们都会执行。

【问题讨论】:

    标签: vue.js vuejs2


    【解决方案1】:

    注意return (key, options) =&gt; ... 位:它返回一个函数! 因此,您在上面编写的模板调用首先检索计算属性(一个函数)的值,然后使用一些参数调用它。

    实际上,抓取$i18n 对象的所有前期准备工作都完成了一次,但如果任何参数发生变化(例如this.lang),转换仍然会发生变化。

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 2017-10-16
      • 2020-01-06
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多