Manoj Damor

Unlock the World of Coding with Coding Funda

How to Add Vue.js 3 into Laravel 11

How to Add Vue.js 3 into Laravel 11

How to Add Vue.js 3 into Laravel 11 || Manoj Damor

Setting up Laravel 11 with Vue.js involves several steps to integrate the backend framework (Laravel) with the frontend framework (Vue). Here is a detailed step-by-step guide.


Prerequisites

Before starting, make sure you have the following installed on your machine:

  • PHP >= 8.1
  • Composer (PHP package manager)
  • Node.js and npm (JavaScript runtime and package manager)
  • Git (optional, for version control)

Step 1: Install Laravel 11

  1. Open your terminal and run the following command to create a new Laravel project:
   composer create-project laravel/laravel example-app
  1. Navigate into the project directory:
   cd example-app
  1. Run the Laravel development server to ensure everything works:
   php artisan serve

Visit http://localhost:8000 in your browser. You should see the Laravel welcome page.


Step 2: Install and Configure Vue.js

  1. Install Laravel Breeze (optional but recommended). This will provide authentication scaffolding along with Vue.js.
   composer require laravel/breeze --dev
   php artisan breeze:install vue
  1. Install Node.js dependencies:
   npm install
  1. Build your frontend assets:
   npm run dev

This will compile the Vue.js components and other frontend assets.


Step 3: Configure Vite for Vue.js in Laravel

Laravel 11 uses Vite for asset bundling instead of Laravel Mix. The Vue configuration is already included when using Breeze, but you can also configure it manually.

  1. Open vite.config.js and ensure the following settings are present:
   import { defineConfig } from 'vite';
   import vue from '@vitejs/plugin-vue';
   import laravel from 'laravel-vite-plugin';

   export default defineConfig({
     plugins: [
       laravel({
         input: ['resources/css/app.css', 'resources/js/app.js'],
         refresh: true,
       }),
       vue(),
     ],
   });
  1. Install the Vue plugin for Vite:
   npm install @vitejs/plugin-vue

Step 4: Create Vue Components

  1. Create a new Vue component inside resources/js/Components: resources/js/Components/ExampleComponent.vue:
   <template>
     <div>
       <h1>Hello from Vue.js!</h1>
       <button @click="sayHello">Click Me</button>
     </div>
   </template>

   <script>
   export default {
     methods: {
       sayHello() {
         alert("Hello from Vue.js!");
       },
     },
   };
   </script>

   <style scoped>
   h1 {
     color: blue;
   }
   </style>
  1. Import and use this component inside resources/js/app.js: resources/js/app.js:
   import { createApp } from 'vue';
   import ExampleComponent from './Components/ExampleComponent.vue';

   const app = createApp({});
   app.component('example-component', ExampleComponent);
   app.mount('#app');
  1. Update the Blade template to include this Vue component: resources/views/welcome.blade.php:
   <!DOCTYPE html>
   <html lang="en">
   <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Laravel with Vue.js</title>
     @vite(['resources/css/app.css', 'resources/js/app.js'])
   </head>
   <body>
     <div id="app">
       <example-component></example-component>
     </div>
   </body>
   </html>

Step 5: Run the Application

  1. Rebuild the frontend assets:
   npm run dev
  1. Run the Laravel development server:
   php artisan serve
  1. Open http://localhost:8000 in your browser. You should see your Vue component rendered with the button that triggers an alert.

Step 6: Enable Hot Reloading (Optional)

If you want hot reloading for better development experience, open another terminal tab and run:

npm run dev

This ensures that changes to your Vue components or frontend assets will be reflected immediately without needing a page refresh.


Troubleshooting Tips

  1. Dependencies Issue: If you face errors while installing npm packages, try:
   npm install --force
  1. CORS Issues: If you’re making API calls between Laravel and Vue, ensure that you handle CORS properly by configuring the Laravel cors.php file.
  2. Production Build: For production, you need to build assets with:
   npm run build

Conclusion

You have now successfully set up Laravel 11 with Vue.js! With this setup, Laravel will handle your backend logic, while Vue.js will handle the frontend. You can create components, handle events, and even build SPAs (Single Page Applications) using this stack.

Check My Social Profile Links

Instagram

Youtube

Website

Linkedin

Android Application

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email
fb-share-icon
Share