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.

TypeError: Cannot read property '$i18n' of undefined

See original GitHub issue

vue & vue-i18n version

ex: ^2.5.3,^7.3.2

Reproduction Link

Steps to reproduce

  • Error: TypeError: Cannot read property ‘$i18n’ of undefined
export default {
    data () {
        const $t = this.$t;
        return {
            msg: $t('msg')
        };
    }
};

  • Normal
export default {
    data () {
        const $t = this.$t.bind(this);
        return {
            msg: $t('msg')
        };
    }
};

What is Expected?

export default {
    data () {
        const $t = this.$t;
        return {
            msg: $t('msg')
        };
    }
};

What is actually happening?

This Not equal to component instance

/* @flow */

export default function extend (Vue: any): void {
  Vue.prototype.$t = function (key: Path, ...values: any): TranslateResult {
    const i18n = this.$i18n
    return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
  }

  Vue.prototype.$tc = function (key: Path, choice?: number, ...values: any): TranslateResult {
    const i18n = this.$i18n
    return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values)
  }

  Vue.prototype.$te = function (key: Path, locale?: Locale): boolean {
    const i18n = this.$i18n
    return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
  }

  Vue.prototype.$d = function (value: number | Date, ...args: any): DateTimeFormatResult {
    return this.$i18n.d(value, ...args)
  }

  Vue.prototype.$n = function (value: number, ...args: any): NumberFormatResult {
    return this.$i18n.n(value, ...args)
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

20reactions
cn-xufeicommented, Aug 20, 2018

Problems still exist. “vue-i18n”: “^8.0.0”

6reactions
denisinvadercommented, Jun 24, 2019

it’s very annoying, especially in render functions. I have to write

const $t = key => this.$t(key);
// or
const $t = this.$t.bind(this);

instead of

const { $t, /* necessary props and data */ } = this;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue-i18n - npm run serve fails with error TypeError: Cannot ...
I can give the following stack trace: TypeError: Cannot read property 'i18n' of undefined at module.exports (..\node_modules\vue-cli-plugin-i18n ...
Read more >
How to fix Vue I18n Uncaught TypeError - Our Code World
How to fix Vue I18n Uncaught TypeError: Cannot read property 'config' of undefined. I've been working on a Vue.js app that needs to...
Read more >
Uncaught (in promise) TypeError: Cannot read property 'i18n ...
Uncaught (in promise) TypeError: Cannot read property 'i18n' of undefined and the UI is broken. Storeview setting is as below:.
Read more >
Using Vue18n can't get it to work - Laracasts
... Error in render: “TypeError: Cannot read property '_t' of undefined”. I don't know how to get this to work. I installed it...
Read more >
Cannot read property '$i18n' of undefined when using Vue ...
[Solved]-Cannot read property '$i18n' of undefined when using Vue Test Utils-Vue.js ... The problem was I was referencing this in the props. Props...
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