integración dinamica componentes y json con idioma

This commit is contained in:
María
2025-07-30 08:04:58 +02:00
committed by María
parent fb4c831c06
commit aa554907d4
4 changed files with 72 additions and 36 deletions

View File

@@ -1,24 +1,29 @@
<template>
<div>
<div>
<h1>{{ content.title }}</h1>
<p>{{ content.body }}</p>
</div>
<p>Idioma actual: {{ $store.getters['langcode'] }}</p>
<p>Ruta actual: {{ $route.params.langcode }}</p>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" @click="cambiarIdioma">
{{ $store.getters['langcode'] === 'es' ? 'Cambiar a Català' : 'Canviar a espanyol' }}
</button>
</div>
<main v-if="landingComponents" class="min-h-screen relative">
<template v-for="(component, index) in landingComponents" :key="`component-${component.component}-${index}`">
<component
:is="component.component"
v-if="component"
v-bind="component.props" />
</template>
<!-- <div class="fixed bottom-0 right-0 z-50 p-2">
<ButtonCTA @click="changeLang" >
Cambiar Idioma
</ButtonCTA>
</div> -->
</main>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import HeroHome from '~/components/HeroHome.vue';
export default {
components: {
HeroHome,
},
data() {
return {
content: {},
landingComponents: []
};
},
computed: {
@@ -28,26 +33,26 @@ export default {
const langcode = this.$route.params.langcode;
try {
const res = await import(`~/data/${langcode}/index.json`);
this.content = res.default;
this.landingComponents = res.default;
} catch (err) {
console.error('Error al cargar contenido:', err);
this.content = {
title: 'Contenido no disponible',
body: 'No se pudo cargar el contenido.',
};
this.landingComponents = [
{
component: 'ErrorComponent',
props: {
message: 'Contenido no disponible',
},
},
];
}
},
methods: {
...mapActions(['setLangcode']),
cambiarIdioma() {
const nuevoIdioma = this.langcode === 'es' ? 'cat' : 'es';
this.setLangcode(nuevoIdioma);
this.$router.push({ path: `/${nuevoIdioma}` });
}
// changeLang() {
// const newLang = this.langcode === 'es' ? 'cat' : 'es';
// this.setLangcode(newLang);
// this.$router.push({ path: `/${newLang}` });
// }
}
}
</script>
<style lang="scss" scoped>
</style>
</script>