How to Add Vue.js to Any Laravel Project
Integrating Vue.js into a Laravel application is straightforward thanks to Vite, Laravel's modern frontend build tool. This guide will walk you through the process step by step.
Works with Laravel 9+ and Laravel 10
Table of Contents
1. Introduction to Vue.js in Laravel
Vue.js is a progressive JavaScript framework for building interactive web interfaces. It's highly compatible with Laravel, especially when using tools like Inertia.js or Livewire.
While Vue can be added to any frontend project, Laravel makes it even easier with Vite, a fast modern bundler.
2. Install Vue.js via NPM/Yarn/Bun
To get started, install Vue and the official Vite plugin:
Using NPM:
Using Yarn:
Using Bun:
3. Configure Vite for Vue.js
Open your vite.config.js
file and update it as follows:
The alias ensures the Vue version with the template compiler is used.
4. Initialize Vue.js in Laravel
Edit resources/js/app.js
To initialize the Vue application:
Next, include the app.js file in your Blade layout and add a root div
for Vue:
5. Verify Vue.js is Working
Let’s create a simple Vue component to test it:
resources/js/components/Counter.vue
Now register the component in app.js
:
Then use the component in your Blade template:
6. Compile Your Code
Finally, start your development server:
With NPM:
With Yarn:
With Bun:
Open your browser, and you should see your Vue counter working!
Conclusion
That’s it! You’ve successfully integrated Vue.js into your Laravel project. You can now build interactive components and dynamic user interfaces with ease.