laravel [Vue 警告]:找不到元素:#app

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41411344/
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-08 17:35:32  来源:igfitidea点击:

[Vue warn]: Cannot find element: #app

laravelvue.jslaravel-5.3

提问by silverFoxA

I have a fresh installed laravel project with all the components updated.

我有一个全新安装的 Laravel 项目,其中所有组件都已更新。

All I did is tried to add app.jsin my welcome.blade.php file, after adding the following I get this error

我所做的只是尝试添加app.js到我的welcome.blade.php 文件中,添加以下内容后出现此错误

[Vue warn]: Cannot find element: #app

[Vue 警告]:找不到元素:#app

I followed this thread, but it's not relevant as my script is at the bottom of the page. https://laracasts.com/discuss/channels/vue/fresh-laravel-setup-and-vue-2-wont-work

我关注了这个线程,但它不相关,因为我的脚本位于页面底部。 https://laracasts.com/discuss/channels/vue/fresh-laravel-setup-and-vue-2-wont-work

Here's my file

这是我的文件

    <!DOCTYPE html>
<html lang="en">
<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>{{$project}} | {{ $title }}</title>

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

    <!-- Styles -->
    <link href="/css/app.css" rel="stylesheet" type="text/css"/>

    <style>
      ...
    </style>
    </head>
    <body class="sticky-nav">
    <div id="nav">
    <div class="display-sm">
        <header class="top-header">
            <span class="menu-icon">?</span>
        </header>
    </div>
        <div class="custom-container">
        <div id="logo" class="float-left">
            <img alt="xyz" src="/images/xyz.png"/><span> xyz</span>
        </div>
        <div id="btn-project" class="float-right">
            <a title="project" href="#" class="btn btn-project">Project</a>
        </div>
    </div>
    </div>
    <div class="sub-container">
    <h1 id="header-title">{{ $header_title }}</h1>
    <h2 id="meta-data"><i class="fa fa"></i>{{$location}} <span id="category"> <i></i>{{$category}} </span></h2>
    <div class="clear"></div>

    </div>
    <script src="/js/app.js"></script>
    <script>
    var wrap = $(".sticky-nav");

    wrap.on("scroll", function (e) {

        if (this.scrollTop > 147) {
            wrap.find('#nav').addClass("fix-nav");
        } else {
            wrap.find('#nav').removeClass("fix-nav");
        }

        });
    </script>
    </body>
    </html>

回答by Alexey Mezenin

Error says it all. Vue can't find #appelement, so you need to add it:

错误说明了一切。Vue 找不到#app元素,所以需要添加:

<div id='app'></div>

https://vuejs.org/v2/guide/

https://vuejs.org/v2/guide/

回答by Vladimir Salguero

Verify that your main design file app.blade.php contains a div with the app id just after the <body>tag just like this:

验证您的主设计文件 app.blade.php 包含一个带有 app id 的 div,紧跟在<body>标签之后,就像这样:

<body>
    <div id="app">
    .
    .
    .
    </div>
</body>

回答by Mandeep Kumar

Sometime everything seems got , but we are still getting same error then you have to copy this code and paste in app. blade.

有时一切似乎都得到了,但我们仍然遇到相同的错误,然后您必须复制此代码并粘贴到应用程序中。刀刃。

<head>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>

<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>

<body>
<div id="app">
 .
 .
 .
 .
 .
 .
</div>
</body>

回答by Parth kharecha

Remove deferfrom script and load your script end of section

从脚本中删除defer并加载您的脚本部分结尾

Example

例子

   <script src="{{ asset('js/app.js') }}" defer></script> //Wrong
To
   <script src="{{ asset('js/app.js') }}"></script> //Right

app.blade.php your main file where all your link load

app.blade.php 您的主文件,您的所有链接都在其中加载

<head>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>

<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>

<body>
<div id="app">
 .
 .
 .
 .
 .
 .
</div>
<!--put your all script file here -->
<script src="{{ asset('js/app.js') }}"></script> 
@yield('page-js-script')
</body>

home.blade.php file or your sub file whic extend the main app file

home.blade.php 文件或扩展主应用程序文件的子文件

@extends('layouts.app')

@section('content')
<div class="container">
 .
 .
 .
 .
 .
 .
</div>
@endsection

@section('page-js-script')
<script type="text/javascript">
    $(document).ready(function() {
        alert("Settings page was loaded");
    });
</script>
@stop