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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43311010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:45:41  来源:igfitidea点击:

how do I load a Vue component in Laravel 5.4?

laravelvue.jscomponentsvuejs2laravel-5.4

提问by Simon Suh

I've run npm run watch and received the message

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

This dependency was not found:

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

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

So I ran npm install --save but still get the same message.

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

From my understanding, I need npm run or npm run watch to load the Vue files in Laravel.

根据我的理解,我需要 npm run 或 npm run watch 在 Laravel 中加载 Vue 文件。

For example, in 'resources/assets/js/components/Example.vue' file, I made edits to it but when I load up 'resources/views/welcome.blade.php' file, which I have as follows, the update from Example.vue file does not load.

例如,在“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>

(I only added in the script src link and the div with the 'app' id and the 'example' component)

(我只在脚本 src 链接和带有 'app' id 和 'example' 组件的 div 中添加了)

What must I do to load the changes made to the Example.vue file? After this, I need to create new components in Vue.js in Laravel as well for my application.

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

采纳答案by Amr Aly

You need to run :

你需要运行:

npm install

then :

然后 :

npm run dev 

回答by llobet

Eventually I figured it out what happens. I don't know why exactly but I have got the answer:

最终我想通了会发生什么。我不知道究竟是为什么,但我得到了答案:

You are trying to include a vue component in a non-layout page like welcome.blade.php. Try wrapping this single page in a layout and add your component, in this case <example></example>. Be sure you are linking to your app.js.

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



Updated: This is what happens exactly: Answer.

更新:这正是发生的事情:答案