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

21
components/HeroHome.vue Normal file
View File

@@ -0,0 +1,21 @@
<template>
<div>
<h1>{{ title }}</h1>
<h2>{{ subtitle }}</h2>
</div>
</template>
<script>
export default {
props: {
title: {
type: String,
default: '',
},
subtitle: {
type: String,
default: '',
},
},
}
</script>

View File

@@ -1,4 +1,9 @@
{ [
"title": "Benvingut", {
"body": "Aquest és el contingut en català." "component": "HeroHome",
} "props": {
"title": "Benvingut al Kit",
"subtitle": "Tot per a l'economia social"
}
}
]

View File

@@ -1,4 +1,9 @@
{ [
"title": "Bienvenido", {
"body": "Este es el contenido en español." "component": "HeroHome",
} "props": {
"title": "Bienvenido al Kit",
"subtitle": "Todo para la economía social"
}
}
]

View File

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