如何在 Laravel 5.4 中加载 Vue 组件?

how do I load a Vue component in Laravel 5.4?

我 运行 npm 运行 观看并收到消息

This dependency was not found:

*  in ./resources/assets/js/app.js

To install it, you can run: npm install --save 

所以我 运行 npm install --save 但仍然收到相同的消息。

据我了解,我需要 npm 运行 或 npm 运行 watch 来加载 Laravel 中的 Vue 文件。

例如,在 'resources/assets/js/components/Example.vue' 文件中,我对其进行了编辑,但是当我加载 'resources/views/welcome.blade.php' 文件时,如下所示,来自 Example.vue 文件的更新没有加载。

<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Raleway', sans-serif;
                font-weight: 100;
                height: 100vh;
                margin: 0;
            }

            .full-height {
                height: 100vh;
            }

            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }

            .position-ref {
                position: relative;
            }

            .top-right {
                position: absolute;
                right: 10px;
                top: 18px;
            }

            .content {
                text-align: center;
            }

            .title {
                font-size: 84px;
            }

            .links > a {
                color: #636b6f;
                padding: 0 25px;
                font-size: 12px;
                font-weight: 600;
                letter-spacing: .1rem;
                text-decoration: none;
                text-transform: uppercase;
            }

            .m-b-md {
                margin-bottom: 30px;
            }
        </style>
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                    @if (Auth::check())
                        <a href="{{ url('/home') }}">Home</a>
                    @else
                        <a href="{{ url('/login') }}">Login</a>
                        <a href="{{ url('/register') }}">Register</a>
                    @endif
                </div>
            @endif

              <div id="app">
                <example></example>
              </div>

            <div class="content">
                <div class="title m-b-md">
                    Laravel
                </div>

                <div class="links">
                    <a href="https://laravel.com/docs">Documentation</a>
                    <a href="https://laracasts.com">Laracasts</a>
                    <a href="https://laravel-news.com">News</a>
                    <a href="https://forge.laravel.com">Forge</a>
                    <a href="https://github.com/laravel/laravel">GitHub</a>
                </div>
            </div>
        </div>


        <script src='js/app.js'></script>
    </body>
</html>

(我只在脚本 src link 和 div 中添加了 'app' id 和 'example' 组件)

我必须怎样做才能加载对 Example.vue 文件所做的更改?之后,我还需要在 Laravel 中的 Vue.js 中为我的应用程序创建新组件。

您需要 运行 :

npm install

然后 :

npm run dev 

最后我明白了会发生什么。我不知道为什么,但我得到了答案:

您正试图在 welcome.blade.php 这样的非布局页面中包含一个 vue 组件。尝试将此单个页面包装在布局中并添加您的组件,在本例中为 <example></example>。确保链接到 app.js.


已更新: 这正是发生的事情:.