vue- Laravel con Inertia.js
Laravel con Inertia.js, no Vue directamente. ¡Es diferente! Te explico cómo funciona y dónde registrar los componentes.
🔍 PRIMERO: ¿Qué tienes?
Tienes Inertia.js, que es un "puente" entre Laravel y Vue. En lugar de usar app.component() como en Vue puro, Inertia maneja las páginas de forma diferente.
📂 DÓNDE REGISTRAR COMPONENTES CON INERTIA
Opción 1: Registro Global (Recomendado para componentes reutilizables)
// 📁 resources/js/app.js
import './bootstrap';
import '../css/app.css';
import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy';
// 🔥 IMPORTAS TUS COMPONENTES AQUÍ
import TaskList from './Components/TaskList.vue'; // ← AQUÍ
import Boton from './Components/Boton.vue'; // ← Y AQUÍ
const appName = import.meta.env.VITE_APP_NAME || 'Laravel';
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
setup({ el, App, props, plugin }) {
// 🔥 CREAS LA APP Y REGISTRAS LOS COMPONENTES
const app = createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue);
// 🔥 REGISTRAS TUS COMPONENTES GLOBALES AQUÍ
app.component('task-list', TaskList); // ← AQUÍ
app.component('boton', Boton); // ← Y AQUÍ
return app.mount(el);
},
progress: {
color: '#4B5563',
},
});📍 Ubicación exacta del código
createInertiaApp({
// ... configuración ...
setup({ el, App, props, plugin }) {
// 1. Creas la app
const app = createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue);
// 2. 🔥 REGISTRAS TUS COMPONENTES AQUÍ (entre .use() y .mount())
app.component('task-list', TaskList);
app.component('boton', Boton);
// ... más componentes
// 3. Montas la app
return app.mount(el);
},
});🎯 EJEMPLO PRÁCTICO COMPLETO
Paso 1: Crear el componente
<!-- 📁 resources/js/Components/TaskList.vue -->
<template>
<div class="grid grid-cols-2 gap-4">
<div v-for="tarea in tareas" :key="tarea.id"
class="bg-white p-4 rounded shadow">
✅ {{ tarea.titulo }}
</div>
</div>
</template>
<script setup>
defineProps({
tareas: Array
});
</script>Paso 2: Registrar en app.js
// 📁 resources/js/app.js
import './bootstrap';
import '../css/app.css';
import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy';
// 🔥 1. IMPORTAS EL COMPONENTE
import TaskList from './Components/TaskList.vue';
const appName = import.meta.env.VITE_APP_NAME || 'Laravel';
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
setup({ el, App, props, plugin }) {
// 🔥 2. CREAS LA APP
const app = createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue);
// 🔥 3. REGISTRAS EL COMPONENTE (AQUÍ)
app.component('task-list', TaskList);
// 🔥 4. MONTAS
return app.mount(el);
},
progress: {
color: '#4B5563',
},
});Paso 3: Usar en una página Blade
{{-- 📁 resources/views/tareas.blade.php --}}
<!DOCTYPE html>
<html>
<head>
@vite('resources/css/app.css')
</head>
<body>
<div id="app">
{{-- Usas el componente --}}
<task-list :tareas='@json($tareas)'></task-list>
</div>
@vite('resources/js/app.js')
</body>
</html>🔄 VERSIÓN CORTA (Solo lo que cambias)
// 📁 resources/js/app.js
import './bootstrap';
import '../css/app.css';
import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/vue3';
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers';
import { ZiggyVue } from '../../vendor/tightenco/ziggy';
// ✅ IMPORTA TUS COMPONENTES
import TaskList from './Components/TaskList.vue';
const appName = import.meta.env.VITE_APP_NAME || 'Laravel';
createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
setup({ el, App, props, plugin }) {
const app = createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue);
// ✅ REGISTRA TUS COMPONENTES AQUÍ
app.component('task-list', TaskList);
return app.mount(el);
},
progress: {
color: '#4B5563',
},
});🆚 DIFERENCIA ENTRE VUE PURO E INERTIA
| Vue Puro (Breeze) | Inertia (Jetstream) |
|---|---|
app.component('mi', Componente) | app.component('mi', Componente) |
Se registra en app.js | Se registra en app.js (igual) |
Usas en *.blade.php | Usas en *.blade.php (igual) |
| Diferencias: | |
Usas @vite('resources/js/app.js') | Usas @vite('resources/js/app.js') (igual) |
Tienes app.mount('#app') | Tienes createInertiaApp() |
¡La forma de registrar componentes es EXACTAMENTE LA MISMA!
💡 REGISTRO LOCAL (Dentro de una página)
Si NO quieres que el componente esté disponible globalmente, puedes registrarlo dentro de una página específica:
Opción A: En una página Vue (Sin <script setup>)
<!-- 📁 resources/js/Pages/Dashboard.vue -->
<template>
<div>
<TaskList :tareas="tareas" />
</div>
</template>
<script>
import TaskList from '@/Components/TaskList.vue';
export default {
components: {
TaskList // ← Registro LOCAL
},
props: {
tareas: Array
}
}
</script>Opción B: En una página Vue (Con <script setup>)
<!-- 📁 resources/js/Pages/Dashboard.vue -->
<template>
<div>
<TaskList :tareas="tareas" />
</div>
</template>
<script setup>
import TaskList from '@/Components/TaskList.vue'; // ← Solo importar (se registra automáticamente)
defineProps({
tareas: Array
});
</script>📁 ESTRUCTURA COMPLETA PARA INERTIA
📁 resources/
📁 js/
📁 Components/ ← COMPONENTES REUTILIZABLES
│ 📄 TaskList.vue
│ 📄 Boton.vue
│ 📄 Tarjeta.vue
│
📁 Pages/ ← PÁGINAS COMPLETAS
│ 📄 Dashboard.vue
│ 📄 Welcome.vue
│ 📄 Profile.vue
│
📄 app.js ← REGISTRO GLOBAL
📄 bootstrap.js🎯 RESUMEN PRÁCTICO
¿Dónde registro el componente?
En resources/js/app.js, dentro de setup(), ANTES de app.mount():
setup({ el, App, props, plugin }) {
const app = createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue);
// 🔥 AQUÍ REGISTRAS
app.component('task-list', TaskList);
return app.mount(el);
}¿Y si quiero registro local?
En la página Vue donde lo vas a usar:
<script setup>
import TaskList from '@/Components/TaskList.vue';
// ¡Ya está registrado localmente!
</script>🚀 EJEMPLO DE REGISTRO MÚLTIPLE
// resources/js/app.js
import TaskList from './Components/TaskList.vue';
import Boton from './Components/Boton.vue';
import Tarjeta from './Components/Tarjeta.vue';
import Modal from './Components/Modal.vue';
setup({ el, App, props, plugin }) {
const app = createApp({ render: () => h(App, props) })
.use(plugin)
.use(ZiggyVue);
// Registras todos
app.component('task-list', TaskList);
app.component('boton', Boton);
app.component('tarjeta', Tarjeta);
app.component('modal', Modal);
return app.mount(el);
}❓ ¿CUÁL USAR?
| Situación | Recomendación |
|---|---|
| Componente que usas en varias páginas | Registro Global en app.js |
| Componente que usas solo en UNA página | Registro Local en la página |
| Componente muy genérico (botones, inputs) | Registro Global |
| Componente específico de una sección | Registro Local |
✅ PASOS RÁPIDOS PARA REGISTRAR
# 1. Creas el componente
touch resources/js/Components/MiComponente.vue
# 2. Editas app.js y agregas:
# import MiComponente from './Components/MiComponente.vue';
# app.component('mi-componente', MiComponente);
# 3. Usas en Blade:
# <mi-componente></mi-componente>
# 4. ¡Listo!¿Te queda claro ahora? Con Inertia es IGUAL que con Vue puro, solo cambia el lugar exacto dentro de app.js. ¡Pregúntame si tienes más dudas
Comentarios
Publicar un comentario