To integrate Vue components within a Blade view in a Laravel 10 application using Vite, you need to ensure that Vue is properly initialized and that your Vue components are registered so that they can be used within your Blade templates. Here's a step-by-step guide to achieve this:
- Install Vue and Vite: It seems you've already done this, but for completeness, you would use the following commands:
npm install vue@next
npm install @vitejs/plugin-vue
-
Configure Vite: Ensure that your
vite.config.jsfile is set up to handle Vue files. It should include the Vue plugin:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
// other configurations...
});
-
Create a Vue Component: Create your Vue component, for example,
DaysGrid.vue:
<template>
<div>
<h1 class="text-3xl font-bold underline text-center">
Calendar VUE component dayGrid placeholder
</h1>
</div>
</template>
<script>
export default {
name: 'DaysGrid',
// component logic...
};
</script>
-
Register the Vue Component Globally: In your main JavaScript file (e.g.,
app.js), import and register your Vue component globally:
import { createApp } from 'vue';
import DaysGrid from './components/DaysGrid.vue';
const app = createApp({});
app.component('days-grid', DaysGrid);
app.mount('#app');
-
Modify the Blade Template: In your Blade template, make sure you have a root element with the id that Vue is initializing on (
#appin this case). You can then use your Vue component within this element:
@extends('layouts.default')
@section('content')
<div id="app">
<h1>Calendar Index Blade View</h1>
<days-grid></days-grid>
</div>
@endsection
-
Include the Vite Generated Script: In your Blade layout (e.g.,
layouts/default.blade.php), include the script tag that Vite generates for your JavaScript entry file:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- other head content -->
</head>
<body>
@yield('content')
@vite('resources/js/app.js')
</body>
</html>
- Run Vite: Start Vite for development or build your assets for production:
npm run dev # for development
npm run build # for production
With these steps, your Vue component should be properly initialized within your Blade view, and you should be able to see the content of the Vue component on your page. Remember to check the browser's console for any errors if the component does not render as expected.