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.

Vite + Vue3 (SFC) TypeError: this.$t is not a function

See original GitHub issue

vue & vue-i18n version

  • vue: 3.0.5
  • vue-i18n: 9.0.0-rc.6
  • vite: 2.0.0-beta.69

Hi, I am upgrading my Vue2 project to vue3 and followed the vue 3 and vue-i18n-next docs to setup the project using Vite. However, I get this.$t is not a function error in my SFC vue component. BTW, I am using the legacy options api.

It seems the global $t is not assigned for some reason. Looking for some hints to resolve this issue.

// main.ts
import { createApp } from "vue";
import App from "./App.vue";
...
import { createI18n } from "vue-i18n";

const app = createApp(App);

const i18n = createI18n({
  locale: "en",
  fallbackLocale: "en",
  silentFallbackWarn: true,
  messages: {
    en: {
      title: "Hello",
    },
  },
});

app.use(i18n);
app.mount("#app");
// App.vue

<template>
  <div>{{ $t('title') }}</div>
  ...
</template>

<script lang="ts">
export default {
};
</script>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kazuponcommented, Feb 16, 2021

Thank you for your feedback.

Sorry, poor documentation. If you don’t configure vue-plugin-vue-i18n plugin compositionOnly to false, you need to configure it. https://github.com/intlify/vite-plugin-vue-i18n#compositiononly

When this plugin set to your app, it will work for Composition API as default.

0reactions
tiagoappcommented, Aug 4, 2021

Thank you for your feedback! I’ll try to consider it. 😃

Now that your issue has been resolved, so I close this issue. Thanks

Hi,

I’m not sure what I am missing… but how can I load the SFC messages instead?

// main.js
import { createApp } from "vue";
import { createI18n } from "vue-i18n";

import App from "./App.vue";

const app = createApp(App);

const i18n = createI18n({
  locale: "en",
  fallbackLocale: "en",
});

app.use(i18n);
app.mount("#app");
// App.vue
<i18n lang="yalm">
en:
   hello: 'hello world'
</i18n>

<template>
msg: {{ $t('hello') }}
</template>

I’ve pretty much tried everything… enable and disable legacy mode, set the compositionOnly: false… and the globalInjection: true

If I don’t set globalInjection: true I get the TypeError: _ctx.$t is not a function....

But if i set it to true, then the messages I put inside the SFC doesn’t load.

How can I get this to work? Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: _ctx.$t is not a function when using $t in child ...
Ok in fact I'm trying to use single file component based translations (as described here: https://vue-i18n.intlify.dev/guide/advanced/sfc.html) ...
Read more >
vue 3 typescript Uncaught (in promise) TypeError: this.$on is ...
I think the problem is not related to Vue3. It is related to typescript. Just add this package @types/vue-select .
Read more >
Uncaught TypeError: Object(...) is not a function in Vue3
I have strange error, i don't know what causing this but last time i develop my project was fine. After no reason my...
Read more >
vue cannot set properties of undefined - You.com | The AI ...
Steps to reproduce · 1) Make an instance of an object with an implicitely 'undefined' property in prototype: const someObj = function() {};...
Read more >
Generically Typed Vue Components with Composition API
js is a functional component, and with Vue 3 we can easily just use a single setup function as an argument to defineComponent...
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