-
In the vue2 version one would do something like:
Which when importing in other components results in So now im kinda stumped. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hmm... When is the variable instantiated? I have a class, let's call it class1. Then i have a class called I18nModule, which looks like this: export type I18nType = I18n<LocaleMessages<VueMessageType>, unknown, unknown, false>;
export default class I18nModule {
readonly name = '18n';
i18n!: I18nType;
constructor() {
this.install();
}
install(): void {
this.i18n = createI18n({
legacy: false,
locale: process.env.VUE_APP_I18N_LOCALE || 'en',
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',
messages: this.loadLocaleMessages(),
});
}
/**
* Load locale messages
*
* The loaded `JSON` locale messages is pre-compiled by `@intlify/vue-i18n-loader`,
* which is integrated into `vue-cli-plugin-i18n`.
* See: https://github.com/intlify/vue-i18n-loader#rocket-i18n-resource-pre-compilation
*/
loadLocaleMessages(): LocaleMessages<VueMessageType> {
const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i);
const messages: LocaleMessages<VueMessageType> = {};
locales.keys().forEach((key) => {
const matched = key.match(/([A-Za-z0-9-_]+)\./i);
if (matched && matched.length > 1) {
const locale = matched[1];
messages[locale] = locales(key);
}
});
return messages;
}
} Then i have a class called CoreModule, which looks like this: export default class CoreModule extends Module {
readonly name = 'core';
i18n: I18nType;
constructor(router: Router, store: Store<any>, i18n: I18nType) {
super(router, store);
this.i18n = i18n;
}
install(): void {
createApp(App)
.use(this.i18n)
.use(this.store)
.use(this.router)
.mount('#app');
}
} Lastly i instantiate it all like this: // main.ts
const router = new RouterModule();
const store = new StoreModule();
const i18n = new I18nModule();
// Placed here for testing purposes
const { global } = i18n.i18n;
console.log(global);
export { global };
new CoreModule(router.router, store.store, i18n.i18n).install(); Now. Class1 is used by a SFC component as an instance, so something like:
The instances.ts is then imported in the SFC component. Weird thing is, i just tried with some good old console.log's. It seems the instances console.log is executed first, then the one's in main.ts. Which seems incredibly weird to me. |
Beta Was this translation helpful? Give feedback.
Hmm... When is the variable instantiated?
Because something funky is going on here.
I have a class, let's call it class1. Then i have a class called I18nModule, which looks like this: