question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Vue.t() inside a state of Vuex

See original GitHub issue

Hey guys! I’am trying the following code:

import Vue from 'vue'

export default {
    task: {
        status: [
            { id: 'pending', name: Vue.t('pending') },
            { id: 'done', name: 'Done' }
        ]
    }
}

That is my state.js which is the state of my VUEX! When I try to use the Vue.t function I have the following error:

Uncaught TypeError: _vue2.default.t is not a function

My solution (which I don’t think it’s the best one also good for performance)

Let my state.js like that:

import Vue from 'vue'

export default {
    task: {
        status: [
            { id: 'pending', name: 'pending' },
            { id: 'done', name: 'done' }
        ]
    }
}

And I’ve done the following getters (vuex getter)

import { map } from 'lodash'
import { trans } from 'utils/helpers/translation' // helper for Vue.t(string)

export const getTaskStatus = ({ task }) => map(task.status, (obj) => {
    return { id: obj.id, name: trans(obj.name) }
})

Anyone knows how is the best way to make it work?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
constagorgancommented, Jun 20, 2019

TL;DR: This worked for me: this.$t('navigationLinkMain')

In the component’s file (e.g.: Component.vue):

<template>
  <div>{{ links.navigationLinkMain }}</div>
</template>

<script>
export default {
  name: 'Component',
  data() {
    return {
      links: {
        navigationLinkMain: this.$t('navigationLinkMain')
      }
    };
  }
};
</script>

In i18n.js file:

import Vue from 'vue';
VueI18n from 'vue-i18n';

Vue.use(VueI18n);

export const i18n = new VueI18n({
  locale: 'en', // set locale
  fallbackLocale: 'ro', // set fallback locale
  messages: {
    'en': {
      'navigationLinkMain': 'inreg.co.uk',
    },
    'ro': {
      'navigationLinkMain': 'inreg.ro',
    }
  } // set locale messages
});

And in src/main.js:

(...)
import { i18n } from '@/plugins/i18n';
(...)

new Vue({
  (...)
  i18n,
  render: h => h(App)
}).$mount('#app')
6reactions
Dsweihcommented, Sep 4, 2017

I have tried the other options as my store is imported from the other file. I always got an error message (_vue2.default.t is not a function) when I called Vue.t in /store.js. The Vue instances are not the same in different files, are they? Thank you @tvanier anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

State - Vuex
Vuex uses a single state tree - that is, this single object contains all your application level state and serves as the "single...
Read more >
Using kazupon/vue-i18n inside a state of Vuex - Stack Overflow
That is my state.js which is the state of my VUEX! When I try to use the Vue.t function I have the following...
Read more >
How To Manage State in a Vue.js Application with Vuex
In this application, you are going to iterate through this data to generate cards consisting of the name , abbreviation , city ,...
Read more >
Managing Shared State In Vue 3 - Smashing Magazine
Additionally, Vuex does support development-time tools (via the Vue Tools) to work with the state management including a feature called time- ...
Read more >
State Management in Vue 3 Applications with Vuex - Auth0
Learn how to manage state in Vue 3 Applications using Vuex. ... TL;DR: Shared data/state amongst components in front-end applications can ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found