【问题标题】:how do I load a Vue component in Laravel 5.4?如何在 Laravel 5.4 中加载 Vue 组件?
【发布时间】:2017-09-04 18:42:39
【问题描述】:

我已经运行 npm run watch 并收到消息

This dependency was not found:

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

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

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

据我了解,我需要 npm run 或 npm run 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 链接和 div 中添加了 'app' id 和 'example' 组件)

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

【问题讨论】:

  • 哪个依赖?

标签: laravel vue.js components vuejs2 laravel-5.4


【解决方案1】:

最终我弄清楚了会发生什么。我不知道为什么,但我得到了答案:

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


更新: 这正是发生的事情:Answer

【讨论】:

    【解决方案2】:

    你需要运行:

    npm install
    

    然后:

    npm run dev 
    

    【讨论】:

    • 我这样做了,现在我的 组件根本不显示。
    • 你是如何修改你的示例组件的?
    • 我只是将 div 中的文本从“我是示例组件”更改为“我是现在更改的示例组件”
    • 我加载到welcome.blade.php 文件中的js 文件是js/app.js 但我知道我需要加载resources/assets/js/app.js 文件但我没有不知道怎么做。
    • 您的组件是否已在您的 app.js 文件中注册
    猜你喜欢
    • 2017-10-24
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2021-01-28
    • 2021-11-27
    • 2021-12-25
    相关资源
    最近更新 更多