Closed bernardinosousa closed 3 years ago
Hi! which version of vue are you using? It seems like it is a vuejs issue. Is the mixin properly registered?
Hello! Vue.js - 2.6.12
How do you import vue? The error indicates that "this" is undefined, which is abnormal for a vue2 app,
It works well for me:
import Vue from 'vue'
import {InertiaApp, plugin} from '@inertiajs/inertia-vue';
// @ts-ignore
import {InertiaForm} from 'laravel-jetstream'
import {__, trans, transChoice, setLocale, locales, getLocale, MaticeLocalizationConfig} from "matice"
Vue.use(InertiaForm);
Vue.use(plugin)
Vue.mixin({
methods: {
$trans: trans,
$__: __,
$transChoice: transChoice,
// @ts-ignore
$setLocale(locale: string) {
setLocale(locale);
this.$forceUpdate() // Refresh the vue instance after locale change.
},
// The current locale
$locale() {
return getLocale()
},
$locales() {
return locales()
}
},
});
const root = document.querySelector('#app');
app = new Vue({
// @ts-ignore
render: (h) =>
// @ts-ignore
h(InertiaApp, {
props: {
// @ts-ignore
initialPage: JSON.parse(root.dataset.page),
// @ts-ignore
resolveComponent: (name: string) => require(`./Pages/${name}`).default,
},
}),
// @ts-ignore
}).$mount(root)
The error is not happening on app.js but inside Pages Vue file.
Snippet calling setLocale
<b-dropdown-item v-for="locale in $locales()" :key="locale" @click="$setLocale(locale)">{{ upperCase(locale) }}</b-dropdown-item>
Do not use arrow function in your mixing because this
will not reference your vue component. So replace:
$setLocale: (locale) => {
setLocale(locale);
this.$forceUpdate() // Refresh the vue instance after locale change.
}
By
$setLocale(locale: string) {
setLocale(locale);
this.$forceUpdate() // Refresh the vue instance after locale change.
},
Expected behavior
When changing language should change language
Current behavior
Error when changing language and not working
Snippet calling setLocale
<b-dropdown-item v-for="locale in $locales()" :key="locale" @click="$setLocale(locale)">{{ upperCase(locale) }}</b-dropdown-item>
app.js
Versions