如何在 Laravel 8 中安装 Vue.js [英] How to Install Vue.js in Laravel 8
问题描述
我正在使用 laravel 8
,现在我想安装 Vue.js
.我正在尝试这样
composer 需要 laravel/ui
php artisan ui vue
php artisan ui vue --auth
更新:如果您想完全避免 Laravel ^8.0 应用程序中的 Inertia/Livewire (Alpine.js) 脚手架并改用 Vue - 安装 Laravel UI,很可能会无限期维护.
在 Laravel 应用程序中安装 Vue(和旧的身份验证脚手架)的说明:
- 运行
composer require laravel/ui
- 运行
php artisan ui vue
以安装 Vue. - 运行
php artisan ui vue --auth
以搭建 auth 视图. - 运行
npm install &&npm run dev
但是,如果您仍想将 Vue.js
与 Livewire
脚手架一起使用,请使用以下说明.
重要提示:请注意,Vue.js
会在安装后控制 DOM,分离节点并替换它,移除其他 JS 侦听器.因此,如果您在 Vue 的同一页面上使用 Livewire,则 Livewire
脚手架附带的 Alpine.js
将无法工作.作为解决方法,您可以使用 Livewire VueJS 支持插件.
运行
npm install --save vue
将以下内容添加到您的资源/js/app.js:
window.Vue = require('vue');Vue.component('example-component', require('./components/ExampleComponent.vue').default);const app = new Vue({el: '#app',});
在resources/js/components目录下创建一个ExampleComponent.vue
<div>Hello World.</div></模板><脚本>导出默认{安装(){console.log(安装的示例组件");}};
添加
将
id=app"
添加到布局文件中的或主要
(resources/views/layouts/app.blade.php
)将
添加到您的视图中运行
npm run dev
或npm run watch
最后,打开 devtools,在控制台日志中你应该看到
Example component installed
I am using
laravel 8
and now I want installVue.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 ^8.0 applications and use Vue instead - install Laravel UI, which will most likely be maintained indefinitely.
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
withLivewire
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, theAlpine.js
that comes withLivewire
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 viewRun
npm run dev
ornpm run watch
Finally, open up the devtools, and in the console log you should see
Example component mounted
这篇关于如何在 Laravel 8 中安装 Vue.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文