TypeError: Cannot read property 'apply' of undefined on Vue-i18n

TypeError: Cannot read property 'apply' of undefined on Vue-i18n

我目前在项目中使用 Vue-i18n。

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

有什么方法可以将 $t 改为其他内容吗?也许有一些方法可以覆盖?

例如,而不是:{{ $t('name') }}

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


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

但是如果我也更改了return参数(从$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 ))
  };
};

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