FLUJO COMPLETO DE UN COMPONENTE VUE EN LARAVEL-solo vue

Imagina que es como una cadena de montaje:

text
1. CREAS → 2. REGISTRAS → 3. USAS → 4. VES

📦 PASO 1: CREAR el Componente

Ubicación: resources/js/Components/

Aquí creas el archivo .vue con todo su código (HTML, CSS, JavaScript).

vue
<!-- 📁 resources/js/Components/MiTarjeta.vue -->

<template>
    <div class="tarjeta">
        <h3>{{ titulo }}</h3>
        <p>{{ contenido }}</p>
    </div>
</template>

<script>
export default {
    name: 'MiTarjeta',  // ← Este nombre es importante
    props: ['titulo', 'contenido']
}
</script>

📋 PASO 2: REGISTRAR el Componente

Ubicación: resources/js/app.js

Aquí le dices a Vue: "Oye, quiero que este componente esté disponible en toda la aplicación".

Forma 1: Registro Global (La que usamos)

javascript
// 📁 resources/js/app.js

import './bootstrap';
import { createApp } from 'vue';
import MiTarjeta from './Components/MiTarjeta.vue';  // ← Importas

const app = createApp({});

app.component('mi-tarjeta', MiTarjeta);  // ← Registras con un nombre

app.mount('#app');

¿Qué significa app.component('mi-tarjeta', MiTarjeta)?

  • 'mi-tarjeta' = El nombre que usarás en el HTML (siempre en kebab-case)

  • MiTarjeta = El componente que importaste

Forma 2: Registro Local (En un componente específico)

vue
<template>
    <div>
        <!-- Usas el componente -->
        <MiTarjeta titulo="Hola" contenido="Mundo" />
    </div>
</template>

<script>
import MiTarjeta from './Components/MiTarjeta.vue';  // ← Importas

export default {
    components: {
        MiTarjeta  // ← Registras solo para este componente
    }
}
</script>

🏠 PASO 3: USAR el Componente

Ubicación: resources/views/*.blade.php

Aquí pones el componente en tu HTML.

blade
{{-- 📁 resources/views/mi-pagina.blade.php --}}

<!DOCTYPE html>
<html>
<head>
    @vite('resources/css/app.css')
</head>
<body>
    <div id="app">
        {{-- ¡Aquí usas el componente! --}}
        <mi-tarjeta 
            titulo="Mi primera tarjeta" 
            contenido="Este es el contenido de la tarjeta"
        ></mi-tarjeta>
    </div>
    
    @vite('resources/js/app.js')
</body>
</html>

🌐 PASO 4: VER el Resultado

Ubicación: Navegador

Cuando abres http://localhost:8000/mi-pagina, ves:

text
┌─────────────────────┐
│  Mi primera tarjeta  │
│  Este es el conteni- │
│  do de la tarjeta    │
└─────────────────────┘

🎯 EJEMPLO PRÁCTICO COMPLETO

Escenario: Quieres crear un contador

1. CREAS el componente

vue
<!-- 📁 resources/js/Components/Contador.vue -->

<template>
    <div class="contador">
        <h2>Contador: {{ contador }}</h2>
        <button @click="incrementar">+1</button>
        <button @click="decrementar">-1</button>
    </div>
</template>

<script>
export default {
    name: 'Contador',
    data() {
        return {
            contador: 0
        }
    },
    methods: {
        incrementar() {
            this.contador++;
        },
        decrementar() {
            this.contador--;
        }
    }
}
</script>

2. REGISTRAS en app.js

javascript
// 📁 resources/js/app.js

import './bootstrap';
import { createApp } from 'vue';
import Contador from './Components/Contador.vue';  // ← Importas

const app = createApp({});

app.component('mi-contador', Contador);  // ← Registras

app.mount('#app');

3. USAS en Blade

blade
{{-- 📁 resources/views/contador.blade.php --}}

<!DOCTYPE html>
<html>
<head>
    @vite('resources/css/app.css')
</head>
<body>
    <div id="app">
        {{-- Usas el componente --}}
        <mi-contador></mi-contador>
    </div>
    
    @vite('resources/js/app.js')
</body>
</html>

4. CREAS la RUTA

php
// 📁 routes/web.php

Route::get('/contador', function () {
    return view('contador');
});

5. VES el Resultado

text
┌─────────────────────┐
│  Contador: 0         │
│  [+1]  [-1]          │
└─────────────────────┘

¡Y cada vez que haces clic, el número cambia!


📊 DIAGRAMA DEL FLUJO

text
┌─────────────────────────────────────────────────────────────────┐
│                           TU PROYECTO                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  1. 📁 resources/js/Components/Contador.vue                    │
│     ┌─────────────────────────────────┐                        │
│     │  <template>                     │                        │
│     │    <h2>Contador: 0</h2>        │                        │
│     │    <button>+1</button>         │                        │
│     │  </template>                   │                        │
│     └─────────────────────────────────┘                        │
│                      │                                          │
│                      ▼                                          │
│  2. 📁 resources/js/app.js                                     │
│     ┌─────────────────────────────────┐                        │
│     │  import Contador from './...'   │                        │
│     │  app.component('contador', ...) │  ← REGISTRAS          │
│     │  app.mount('#app')              │                        │
│     └─────────────────────────────────┘                        │
│                      │                                          │
│                      ▼                                          │
│  3. 📁 resources/views/contador.blade.php                     │
│     ┌─────────────────────────────────┐                        │
│     │  <div id="app">                 │                        │
│     │    <contador></contador>        │  ← USAS                │
│     │  </div>                         │                        │
│     │  @vite('resources/js/app.js')   │                        │
│     └─────────────────────────────────┘                        │
│                      │                                          │
│                      ▼                                          │
│  4. 🌐 Navegador en http://localhost:8000/contador            │
│     ┌─────────────────────────────────┐                        │
│     │  Contador: 0                    │  ← VES                │
│     │  [+1]  [-1]                     │                        │
│     └─────────────────────────────────┘                        │
└─────────────────────────────────────────────────────────────────┘

🔑 REGLA DE ORO

PasoAcciónUbicación
1CREAR el componenteresources/js/Components/
2REGISTRAR en Vueresources/js/app.js
3USAR en el HTMLresources/views/*.blade.php
4VER el resultadoNavegador

💡 EJEMPLO CON TUS TAREAS DE 2 COLUMNAS

1. CREAS TaskList.vue

vue
<!-- resources/js/Components/TaskList.vue -->
<template>
    <div class="grid grid-cols-2 gap-4">
        <div v-for="tarea in tareas" :key="tarea.id">
            ✅ {{ tarea.titulo }}
        </div>
    </div>
</template>

<script>
export default {
    props: ['tareas']
}
</script>

2. REGISTRAS en app.js

javascript
import TaskList from './Components/TaskList.vue';
app.component('task-list', TaskList);

3. USAS en Blade

blade
<div id="app">
    <task-list :tareas='@json($tareas)'></task-list>
</div>

4. VES el resultado

text
┌───────────┬───────────┐
│ ✅ Tarea 1 │ ✅ Tarea 2 │
├───────────┼───────────┤
│ ✅ Tarea 3 │ ✅ Tarea 4 │
└───────────┴───────────┘

PREGUNTAS FRECUENTES

¿Por qué registrarlo en app.js?

Porque app.js es el punto de entrada de Vue. Todo lo que registres aquí estará disponible en toda la aplicación.

¿Puedo tener muchos componentes?

¡Sí! Puedes tener cientos. Solo importas y registras cada uno:

javascript
import Componente1 from './Components/Componente1.vue';
import Componente2 from './Components/Componente2.vue';
import Componente3 from './Components/Componente3.vue';

app.component('componente-1', Componente1);
app.component('componente-2', Componente2);
app.component('componente-3', Componente3);

¿Puedo usar un componente dentro de otro?

¡Sí! Eso se llama composición. Puedes tener componentes anidados:

vue
<!-- TarjetaPadre.vue -->
<template>
    <div>
        <TarjetaHija mensaje="Hola" />
    </div>
</template>

<script>
import TarjetaHija from './TarjetaHija.vue';

export default {
    components: {
        TarjetaHija
    }
}
</script>

¿Por qué no veo los cambios?

  1. Asegúrate que npm run dev esté corriendo

  2. Recarga con Ctrl + Shift + R

  3. Si sigue, presiona r + Enter en la terminal de npm


🎯 RESUMEN VISUAL

text
                    CREAS
                      ↓
    ┌─────────────────────────────────┐
    │  resources/js/Components/       │
    │  ┌───────────────────────────┐  │
    │  │  MiComponente.vue         │  │
    │  │  <template>               │  │
    │  │    Hola mundo             │  │
    │  │  </template>              │  │
    │  └───────────────────────────┘  │
    └─────────────────────────────────┘
                      ↓
                   REGISTRAS
                      ↓
    ┌─────────────────────────────────┐
    │  resources/js/app.js            │
    │  ┌───────────────────────────┐  │
    │  │  import MiComponente      │  │
    │  │  app.component('mi-',...) │  │
    │  └───────────────────────────┘  │
    └─────────────────────────────────┘
                      ↓
                    USAS
                      ↓
    ┌─────────────────────────────────┐
    │  resources/views/               │
    │  ┌───────────────────────────┐  │
    │  │  <div id="app">           │  │
    │  │    <mi-componente />      │  │
    │  │  </div>                   │  │
    │  └───────────────────────────┘  │
    └─────────────────────────────────┘
                      ↓
                    VES
                      ↓
    ┌─────────────────────────────────┐
    │  🌐 Navegador                   │
    │  ┌───────────────────────────┐  │
    │  │  Hola mundo               │  │
    │  └───────────────────────────┘  │
    └─────────────────────────────────┘

¿Te queda claro el flujo? ¡Pregúntame si tienes dudas

Comentarios

Entradas más populares de este blog

Tareas con Laravel + Inertia + Jetstream

HACER CAMBIOS-AL EJEMPLO POR DEFECTO

Laravel + Inertia - ejemplo