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

Radu12's avatar
Level 1

Vue.js integration with blade

I have an application developed with Laravel 10 and I want to integrate vue.js into blade templates. More exactly, I want to use vue components wherever I need in my templates. For example, I have the following:

app.js

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

const app = createApp(App);

app.component('test-component', TestComponent);
app.component('example-component', ExampleComponent)
app.mount("#app");

App.vue

<template>
    <test-component></test-component>
    <example-component></example-component>
</template>

And the 2 components:

TestComponent.vue

<template>
    <div @click="showMore" class="card card-header col-md-3 col-sm-6 text-center m-auto"
         style="cursor: pointer;">
        Show more <span class="icon-arrow-circle-down"></span>
    </div>
</template>

<script setup>
    const showMore = () => {
        alert('show');
    }
</script>

ExampleComponent.vue

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Example Component</div>

                    <div class="card-body">
                        I'm an example component.
                    </div>
                    <button @click="showAlert">Show alert</button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
    const showAlert = () => {
        alert('example');
    }
</script>

If I write the following <div id="app"></div> in an app.blade.php, the rendering and javascript work as expected. However, what if I need to use one component in a file and the other component in another file? I tried to place the components in separate files but it doesn't work.

0 likes
7 replies
aleahy's avatar

In this situation you want to dynamically load the component in your app.js file. Then it will build the component into a separate file that is pulled in when it is needed.

import {createApp, defineAsyncComponent } from 'vue';

...

app.component('example-component', defineAsyncComponent(() => import('./components/ExampleComponent.vue')));
aleahy's avatar

@Radu12 You will still need <div id="app"></div> in your template so it has somewhere to put it. Did you leave that in?

Radu12's avatar
Level 1

@aleahy Yes, I added it in app.blade.php as the first child of <body> and I also added the tags of the 2 components in App.vue file.

MaverickChan's avatar

delete App.vue and just use component in blade file directly.

and remove createApp(App) change to

const app = createApp() //leave blank, if you are not use a pure vue app
//then regisiter your component for blade
app.component('test-component', TestComponent);
app.component('example-component', ExampleComponent)
//then mount the app to id="app"
app.mount("#app");

now , for example , you can use your vue component in blade file everywhere.

MaverickChan's avatar

@Radu12 delete node_modules then npm install again after that , don't forget run npm run build or npm run dev

my code is fine , and i use it everyday

Please or to participate in this conversation.