vue.js - 在 Vuex 商店中使用 i18n-nuxt $t

标签 vue.js internationalization vuex nuxt-i18n

我的问题有点具体。我在这个项目中使用 Vuex 和 Nuxt。对于依赖项,我们使用 i18n-nuxt 但不知何故我无法在 $store 中使用 $t('some.translation') 而在组件中它工作得很好。我尝试了所有我能想到的可能组合,但结果仍然导致我犯同样的错误。

ReferenceError $t is not defined or Cannot read the property of i18n

所以在这种情况下,我可以使用一些帮助来解决这个问题,或者如果有人向我展示如何使用 i18n 作为过滤器,那将是完美的。 (我认为这是唯一的解决方法。)

例如这个代码块来自$store.state

  sortMethods: [
{ id: 'airline', name: this.i18n.$t('Airline'), asc: 'A', desc: 'Z', defaultDirection: 'asc' },

你可以想象我无法在原地翻译它们。

最佳答案

可以在 this.$i18n.t 的突变和操作中使用 t。例如:

export const mutations = {
  foo() {
    console.log(this.$i18n.t("bar"));
  }
}

但是我还没有找到在 state 或 getter 中使用它的方法,因为它们无权访问 this

关于vue.js - 在 Vuex 商店中使用 i18n-nuxt $t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65488981/

相关文章:

javascript - Vue react 性问题

node.js - 如何修复错误 "net::ERR_SSL_SERVER_CERT_BAD_FORMAT"将 vuejs 和 nodejs 与 https 和 express 一起使用时

javascript - Vue 绑定(bind)覆盖元素属性

laravel - 如何在 Laravel 中更新 Example.vue?

unit-testing - 在单元测试中使用 webapp2 i18n

javascript - Vue add i18n 返回错误 TypeError : Cannot read property 'minVersion' of undefined

vue.js - NuxtServerInit 无法在 Vuex 模块模式下工作 - Nuxt.js

javascript - 为什么这些查询独立工作而不是一起工作?

laravel - 如何在 Laravel Echo 中获取连接状态/其他事件

angularjs - 是否可以在运行时动态切换 AngularJS 的国际化语言?