laravel 9
and now I want install Vue.js
. I am trying like thiscomposer require laravel/ui
php artisan ui vue
php artisan ui vue --auth
UPDATE: If you want to completely avoid Inertia / Livewire (Alpine.js) scaffolding in your Laravel ^9.0 applications and use Vue instead - install Laravel UI, which will most likely be maintained indefinitely (scaled to practical software lifetime).
Instructions for installing Vue (and old auth scaffolding) in your Laravel app:
- run
composer require laravel/ui
- run
php artisan ui vue
for just installing Vue. - run
php artisan ui vue --auth
for scaffolding out the auth views. - run
npm install && npm run dev
How ever, if you still want to use Vue.js
with Livewire
scaffolding, use the following instructions.
IMPORTANT: Please note that Vue.js
takes control of the DOM once installed, detaching nodes and replacing it, removing other JS listeners. So, if you are using Livewire on the same page with Vue, the Alpine.js
that comes with Livewire
scaffolding wont work. As a workaround you can use Livewire VueJS support plugin.
-
run
npm install --save vue
-
Add the following to your resources/js/app.js:
window.Vue = require('vue'); Vue.component('example-component', require('./components/ExampleComponent.vue').default); const app = new Vue({ el: '#app', });
-
Create an ExampleComponent.vue in the resources/js/components directory
<template> <div>Hello World.</div> </template> <script> export default { mounted() { console.log("Example component mounted"); } }; </script>
-
Add
<script src="{{ asset('js/app.js') }}" defer></script>
in the<head>
section of your layout file (resources/views/layouts/app.blade.php
) -
Add
id="app"
to<body>
or main<div>
in your layout file (resources/views/layouts/app.blade.php
) -
Add
<example-component />
to your view -
Run
npm run dev
ornpm run watch
-
Finally, open up the devtools, and in the console log you should see
Example component mounted
In laravel 9 project run following commands to install vue.js
- run
composer require laravel/ui
- Install Vue
php artisan ui vue
- Install Vue with auth
php artisan ui vue --auth
- run
npm install && npm run dev
I wasted so much time with this and don't want people to go through the same, so I will teach you how to install Vue.js and make it work fast.
If you want to start from scratch
replace the LaravelProject
with your project name
laravel new LaravelProject
Install Vue.js on your Laravel 9 application
run the following commands
composer require laravel/ui
php artisan ui vue
php artisan ui vue --auth
npm install
npm run dev
npm run watch
Now that you have installed everything you need, go to a blade view and add the following code inside <body></body>
<div id="app">
<example-component />
</div>
<script src="{{ mix('/js/app.js') }}"></script>
If you did everything right you will see the following text on the rendering of your view
Example Component
Im an example component.
And in your console: Component mounted.
You can install Vue 3 on your laravel project via three different ways:
- Import it as a CDN package on the page
- Download the JavaScript files and host them yourself
- Install it using npm
1. Via CDN
include following code in your HTML file:
<script src="https://unpkg.com/vue@next"></script>
or use the following link for production:
https://cdnjs.com/libraries/vue
2. Download and Host yourself:
Download it from GitHub here:
https://github.com/vuejs/vue-next
3. Via npm
npm is the recommended installation method when building large scale applications with Vue. It pairs nicely with module bundlers such as Webpack (opens new window)or Rollup (opens new window). Vue also provides accompanying tools for authoring Single File Components.
Run this command in your terminal to get the latest stable version
$ npm install vue@next
Followed by:
npm install