Nuxt3 access to useNuxtApp() and i18n from helpers files

234 views Asked by At

I'm relatively new to Nuxt.js 3, having previously worked with Vue and Vuetify2. I've recently initiated a project using Vuetify3 and Nuxt3, encountering a few challenges.

In my previous projects, I relied on helper files for streamlined processes. In the current project, I'm using @nuxtjs/i18n for internationalization and need to access it from the store to translate data fetched from an API.

Additionally, I'd like to leverage useNuxtApp() in helper files to access plugins and utilize them outside of Vue components.

Any guidance on efficiently incorporating these aspects into my Nuxt3 project would be highly appreciated.

I found workaround but I do not know if it is good or not

// plugins/helper.ts
let appInstance; // Declare a variable to store the app instance

export default defineNuxtPlugin((app) => {
  appInstance = app; // Assign the app instance to the variable  
});

// Export the app instance
export const getAppInstance = () => {
  return appInstance;
};

Then I can call it from anywhere like this

import { getAppInstance } from "@/plugins/helprs";

const app = getAppInstance();
alert(app.$i18n.t("hello"));
0

There are 0 answers