因此,我将无法像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);
},
}
但我无法弄清楚他如何使它像函数一样调用,以及他如何获得传入的参数。
我无法使用一种方法,因为它们在每次更新时都会执行。
最佳答案
注意return (key, options) => ...
位:返回一个函数!
因此,您在上面编写的模板调用首先检索计算出的属性(一个函数)的值,然后使用一些参数来调用它。
实际上,获取$i18n
对象的所有前期准备都完成了一次,但是如果任何参数发生更改(例如this.lang
),转换仍然可以更改。
关于vue.js - Vue Mixin计算的函数传递参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47519838/