Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mcadio's avatar

How to use vue in blade components with laravel 10 & vite

I just installed a fresh laravel 10 app with vite, vue, and tailwindcss. It's setup for the default route to point to one vue component.

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

the view file:

<template>
    <div>
        <h1 class="text-3xl font-bold underline text-center">
            Laravel 10 , Vue.js 3 , Tailwind CSS and Vite
        </h1>
    </div>
</template>
<script setup>
import {ref} from 'vue'
const name = ref();
</script>

What I was thinking of doing was converting from blade to Vue one page at a time. Can I convert my calendar page to vue by inserting a vue component into the blade view and stick an id="app" tag in the main div? I'm trying but not able to see the text from the vue component. Only what I put in the blade.

<template>
    <div>
        <h1 class="text-3xl font-bold underline text-center">
            Calendar VUE component dayGrid placeholder
        </h1>
    </div>
</template>
<script>;
</script>
```

```
@extends('layouts.default')
@section('content')
    <div id="apcp">
        <h1>Calendar Index Blade View</h1>
        <days-grid></days-grid>
    </div>
@endsection
```
0 likes
7 replies
LaryAI's avatar
Level 58

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:

  1. 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
  1. Configure Vite: Ensure that your vite.config.js file 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...
});
  1. 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>
  1. 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');
  1. Modify the Blade Template: In your Blade template, make sure you have a root element with the id that Vue is initializing on (#app in 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
  1. 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>
  1. 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.

mcadio's avatar

@LaryAI Reviewing this, it SEEMS like it should be right, but still not displaying my text from the DaysGrid.vue component. When I try to view /api/cal and inspect the page, this is how things show in my head section:

// the @vite('resources/css/app.css') is in my head layout
<script type="module" src="http://[::1]:5173/@vite/client"></script>
<link rel="stylesheet" href="http://[::1]:5173/resources/css/app.css">

my code for index.blade

<!DOCTYPE html>
<html lang="en">
    <head>
        @include('layouts.head')
        @yield('head')
    </head>
    <body>
        <header class="pb-2">
{{--            @include('layouts.header')--}}
        </header>
        <div class="container">
            @yield('heading')
            @yield('content')
            @vite('resources/js/app.js')
        </div>

        @include('layouts.footer')
        @include('layouts.foot')
    @yield('foot')
    </body>
</html>

my code for app.js

import '../css/app.css';

import {createApp} from 'vue'

import App from './App.vue'
import DaysGrid from "./components/DaysGrid.vue";

const app = createApp({})
app.component('days-grid', DaysGrid)
app.component('app',App)
app.mount('#app')

my api route:

Route::get('/cal',[CalendarController::class,'index']);

This is an error I got trying to instead push directly to vue instead of going through blade.

app.js:11 [Vue warn]: Component is missing template or render function. 
  at <App>

My route for a workaround to see if it was blade or not:

Route::get('/cal',function(){
    $calDays = (new CalController)->index();
    return view('app',['days'=>$calDays]);
});

I don't get it.

mcadio's avatar

So my app.js file was setup incorrectly. Changing it to this worked! I can see the component, and I think I now understand how to do multiple pages this way.

import '../css/app.css';

import { createApp } from 'vue';
import App from './components/App.vue';
import DaysGrid from "./components/DaysGrid.vue";

const app = createApp(App); // Pass App component here
app.component('days-grid', DaysGrid); // Register DaysGrid as a global component if needed
app.mount('#app');

MaverickChan's avatar

put this in your layout blade file's head

@vite(['resources/js/app.js','resources/css/app.css'])
mcadio's avatar

@MaverickChan What I don't understand is how I'm supposed to get to show unless I put it inside the actual vue component instead of a blade component.

MaverickChan's avatar

@mcadio What is in your blade file?

how do you put the component now?

and also , when you naming a component , don't use too simple word like app.

mcadio's avatar

@MaverickChan I ended up figuring out that I needed to use vue-router to do what I was looking for, and now I have rebuilt ALMOST my entire site from blade, but in vue. A few api calls, and upgrades and I'll be finished!

Please or to participate in this conversation.