且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Laravel 8 中安装 Vue.js

更新时间:2022-10-19 13:35:05

更新:如果您想完全避免 Laravel ^8.0 应用程序中的 Inertia/Livewire (Alpine.js) 脚手架并改用 Vue - 安装 Laravel UI,很可能会无限期维护.

在 Laravel 应用程序中安装 Vue(和旧的身份验证脚手架)的说明:

  1. 运行 composer require laravel/ui
  2. 运行 php artisan ui vue 以安装 Vue.
  3. 运行 php artisan ui vue --auth 以搭建 auth 视图.
  4. 运行 npm install &&npm run dev


但是,如果您仍想将 Vue.jsLivewire 脚手架一起使用,请使用以下说明.

重要提示:请注意,Vue.js 会在安装后控制 DOM,分离节点并替换它,移除其他 JS 侦听器.因此,如果您在 Vue 的同一页面上使用 Livewire,则 Livewire 脚手架附带的 Alpine.js 将无法工作.作为解决方法,您可以使用 Livewire VueJS 支持插件.


  1. 运行 npm install --save vue

  2. 将以下内容添加到您的资源/js/app.js:

     window.Vue = require('vue');Vue.component('example-component', require('./components/ExampleComponent.vue').default);const app = new Vue({el: '#app',});

  3. 在resources/js/components目录下创建一个ExampleComponent.vue

  4. 添加) 的 部分中的 defer>

  5. id=app" 添加到布局文件中的 或主要

    (resources/views/layouts/app.blade.php)
  6. 添加到您的视图中

  7. 运行 npm run devnpm run watch

  8. 最后,打开 devtools,在控制台日志中你应该看到 Example component installed

  9. I am using laravel 8 and now I want install Vue.js. I am trying like this

    1. composer require laravel/ui
    2. php artisan ui vue
    3. 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:

    1. run composer require laravel/ui
    2. run php artisan ui vue for just installing Vue.
    3. run php artisan ui vue --auth for scaffolding out the auth views.
    4. 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.


    1. run npm install --save vue

    2. 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',
       });
      

    3. 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>
      

    4. Add <script src="{{ asset('js/app.js') }}" defer></script> in the <head> section of your layout file (resources/views/layouts/app.blade.php)

    5. Add id="app" to <body> or main <div> in your layout file (resources/views/layouts/app.blade.php)

    6. Add <example-component /> to your view

    7. Run npm run dev or npm run watch

    8. Finally, open up the devtools, and in the console log you should see Example component mounted

相关阅读

技术问答最新文章