【问题标题】:TypeError: Cannot read property 'apply' of undefined on Vue-i18nTypeError:无法读取 Vue-i18n 上未定义的属性“应用”
【发布时间】:2020-03-31 03:07:31
【问题描述】:

我目前正在为该项目使用 Vue-i18n。

据我所知,所有要翻译的词都需要使用$t

有什么办法可以将$t 改为别的东西吗?也许有一些方法可以覆盖?

例如,代替:{{ $t('name') }}

改成:{{ $ba('name') }}


我设法使用@Alex 方法做到了,但超过了i18n

但是如果我也更改了返回参数(从$t$ba),就会出现错误TypeError: Cannot read property 'apply' of undefined

有没有办法解决这个问题?

function extend (Vue) {
Vue.prototype.$ba = function (key) {
    var values = [], len = arguments.length - 1;
    while ( len-- > 0 ) values[ len ] = arguments[ len + 1 ];

    var i18n = this.$i18n;
    return i18n._ba.apply(i18n, [ key, i18n.locale, i18n._getMessages(), this ].concat( values ))
  };
};

【问题讨论】:

    标签: vue.js vuejs2 internationalization vue-component vue-i18n


    【解决方案1】:

    $t 只是附加到Vue.prototype 的一些变量。您可以在i18n 的源代码中看到它被分配。因此,在您执行Vue.install(i18n) 之后,您可以覆盖所有这些原型并创建您想要的任何名称。

    【讨论】:

    • 嗨@Alex,感谢您的回复。我将它分配在dist over i18n.. 但是如果我将返回参数与Vue.prototype 的变量一起更改,它将出现Cannot read property 'apply' of undefined. 有什么办法可以解决这个问题吗?我已经更新了上述问题的代码。谢谢。
    • $i18n 里面是什么?
    • 我只是按照i18n的原始源代码,只是更改了Vue.prototype.$bareturn i18n._ba.apply。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 2022-06-28
    • 2020-10-09
    • 2021-12-14
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多