简体   繁体   English

Laravel 5 js被app.js阻止

[英]Laravel 5 js is blocking by app.js

I am using a javascript inside home.blade.php it extends layout/app.blade.php 我在home.blade.php使用了一个javascript,它扩展了layout/app.blade.php

@extend(layout.app)

when I extend javascript inside home.blade.php stop working, So I commented out 当我在home.blade.php扩展javascript停止工作时,我注释掉了

<script src="{{ asset('js/app.js') }}" defer></script> in app.blade.php then javascript starts working. app.blade.php <script src="{{ asset('js/app.js') }}" defer></script>然后javascript开始工作。 I am not sure what is the issue can anyone help me to fix this. 我不确定有什么问题可以帮助我解决这个问题。

home.blade.php home.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
   <div class="row justify-content-center">
      <div class="col-md-8">
         <div class="card">
            @if (Auth::user()->email=='admin@admin.com')
            <div class="card-header">Reports</div>
            <div class="card-body">
               <a href="view-reports">View Report</a><br>
               <a href="create-user">Create User</a>
            </div>
         </div>
         @else
         <div class="card-header">Report Form</div>
         <div class="card-body">
            @if (session('status'))
            <div class="alert alert-success">
               {{ session('status') }}
            </div>
            @endif
            <!-- You are logged in! -->
            @if(session()->has('message'))
            <div class="alert alert-success">
               {{ session()->get('message') }}
            </div>
            @endif
            <form action="store" method="post">
               <input type="hidden" name="name" value="{{Auth::user()->name}}">
               <label for="check_log">Checked Logs </label><br>
               <input type="radio" name="check_log" value="yes"> Yes
               <input type="radio" name="check_log" value="no"> No<br><br>
               <label for="verified_ticket"> Checked and Verified for pending tickets </label><br/>
               <input type="radio" name="verified_ticket" value="yes"> Yes
               <input type="radio" name="verified_ticket" value="no"> No<br><br>
               <label for="ticket_found"> Found any ticket that can be done by you </label><br/>
               <input type="radio" name="ticket_found" value="yes"> Yes
               <input type="radio" name="ticket_found" value="no"> No<br><br>
               <div id="box">
                  <label for="ticket_id"> Tickets done by you (INTERNAL Ticket ID'S) </label><br/>
                  <input type="text" name="ticket_id[]" id="name" class="name"><br/><br>
                  <label for="ticket_details"> what did you do</label><br/>
                  <input style="width: 65%; height: 100px;" type="text" name="ticket_details[]" width="200px" height="100px" value=""><br><br>
                  <label for="time"> Time Taken</label><br/>
                  <input type="text" name="time[]" id="name" class="name"><br/><br>
                  <div id="addmore">
                     <a href="#" id="add">Add More Input Field</a>
                  </div>
                  <br><br>
               </div>
               <label for="other_task"> Other Tasks (Should be specify what did you do and time taken for each task) </label><br/>
               <input style="width: 65%; height: 100px;" type="" name="other_task" value=""><br><br>
               <label for="shift_handoff"> Task/Alert that's needs attention from next shift/Dennis </label><br/>
               <input style="width: 65%; height: 100px;" type="" name="shift_handoff" value=""><br><br>
               <input type="checkbox" name="agree" value="agree"> I Agree<br><br>
               <input type="hidden" name="_token" value="{{csrf_token()}}">
               <input type="submit" name="submit" value="submit">
            </form>
            @endif
         </div>
      </div>
   </div>
</div>
@endsection
@section('javascript')
<script type="text/javascript">
   $(document).ready(function(){

       $('#add').click(function(){
           var inp = $('#box');

           var i = $('input').size() + 1;

           $('<div id="box' + i +'"><label for="ticket_id"> Ticket ID </label><br/><input type="text" name="ticket_id[]" id="name" class="name"><br/><br><label for="ticket_details[]"> what did you do</label><br/><input style="width: 65%; height: 100px;" type="text" name="ticket_details[]" width="200px" height="100px" value=""><br><br><label for="time"> Time Taken</label><br/><input type="text" name="time[]" id="name" class="name"><br><br><img src="Minus.png" width="32" height="32" border="0" align="top" class="add" id="remove" /><br><br> <div id="addmore"><a href="#" id="add">Add More Input Field</a></div><br><br></div>').appendTo(inp);

           i++;

       });



       $('body').on('click','#remove',function(){

           $(this).parent('div').remove();

       });

   });
</script> 
@endsection

app.balde.php app.balde.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- CSRF Token -->
      <meta name="csrf-token" content="{{ csrf_token() }}">
      <title>{{ config('app.name', 'Daily report') }}</title>
      <!-- Scripts -->
      <script src="{{ asset('js/app.js') }}" defer></script>
      <!-- Fonts -->
      <link rel="dns-prefetch" href="https://fonts.gstatic.com">
      <link href="https://fonts.googleapis.com/css?family=Raleway:300,400,600" rel="stylesheet" type="text/css">
      <!-- Styles -->
      <link href="{{ asset('css/app.css') }}" rel="stylesheet">
   </head>
   <body>
      <div id="app">
         <nav class="navbar navbar-expand-md navbar-light navbar-laravel">
            <div class="container">
               <a class="navbar-brand" href="{{ url('/') }}">
               {{ config('app.name', 'Laravel') }}
               </a>
               <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
               <span class="navbar-toggler-icon"></span>
               </button>
               <div class="collapse navbar-collapse" id="navbarSupportedContent">
                  <!-- Left Side Of Navbar -->
                  <ul class="navbar-nav mr-auto">
                  </ul>
                  <!-- Right Side Of Navbar -->
                  <ul class="navbar-nav ml-auto">
                     <!-- Authentication Links -->
                     @guest
                     <li><a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a></li>
                     <li><a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a></li>
                     @else
                     <li class="nav-item dropdown">
                        <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                        {{ Auth::user()->name }} <span class="caret"></span>
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                           <a class="dropdown-item" href="{{ route('logout') }}"
                              onclick="event.preventDefault();
                              document.getElementById('logout-form').submit();">
                           {{ __('Logout') }}
                           </a>
                           <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                              @csrf
                           </form>
                        </div>
                     </li>
                     @endguest
                  </ul>
               </div>
            </div>
         </nav>
         <main class="py-4">
            @yield('content')
            <script src="{{ asset('js/app.js') }}"></script>
            @yield('javascript')
         </main>
      </div>
   </body>
</html>

Your app.blade.php should look like following: 您的app.blade.php应如下所示:

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

        <meta name="csrf-token" content="{{ csrf_token() }}">

        <title>{{ config('app.name', 'Test') }}</title>

        <!-- Styles -->
        <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    </head>
    <body>
        <div id="app">
            @yield('content')
        </div>

        <!-- Scripts -->
        <script src="{{ asset('js/app.js') }}"></script>
        @yield('javascript');
    </body>
</html>

The above is your main layout file so to say. 以上是您的主要布局文件。 You want to use that layout file and add content or additional javascript functionality if you want to extend it in your other blade files. 如果要在其他刀片文件中扩展它,您希望使用该布局文件并添加内容或其他JavaScript功能。

To accomplish this in a nice way, we use the @yield('javascript') keyword to add a javascript file or function. 为了以一种很好的方式实现这一点,我们使用@yield('javascript')关键字来添加javascript文件或函数。

So your other blade files which extend this blade file should look like following: 因此,扩展此刀片文件的其他刀片文件应如下所示:

@extends('layouts.app')

@section('content')
   // some content 
@endsection

@section('javascript')
   // your javascript
@endsection

First you are loading the layout blade file and afterwards you can insert your specific content and javascript to each section where you used the keyword yield('xxx') 首先,您要加载布局刀片文件,然后您可以将特定内容和javascript插入到使用关键字yield('xxx')每个部分

After having a look at your source code: 看了你的源代码后:

You are loading jquery in your home.blade.php which is not ideal, if you want to load it you should load it in your main blade php file. 你正在home.blade.php中加载jquery这是不理想的,如果你想加载它你应该加载到你的主刀片php文件中。 But you actually dont need to load it if you are using laravels default app.js file. 但是如果你使用laravels默认的app.js文件,你实际上不需要加载它。 The default app.js file already requires following require('./bootstrap'); 默认的app.js文件已经需要以下require('./bootstrap'); . That file already contains jquery if you did not change it. 如果你没有改变它,那个文件已经包含了jquery。

try {
    window.$ = window.jQuery = require('jquery');

} catch (e) {}

So check if your app.js requires the bootstrap file and check if your bootstrap file includes jquery if yes, you do not need to load jquery in your blade file. 因此,请检查您的app.js需要引导程序文件,如果是,则检查引导程序文件是否包含jquery,您不需要在刀片文件中加载jquery。

Thus you should load the app.js file in your main blade. 因此,您应该在app.js加载app.js文件。 Load it like this at the bottom: 在底部加载它:

      ...
      <script src="{{ asset('js/app.js') }}"></script>
      @yield('javascript')
   </body>
</html>

see https://laravel.com/docs/5.6/blade#stacks 请参阅https://laravel.com/docs/5.6/blade#stacks

for adding script to your child view you need to use stack 要将脚本添加到子视图,您需要使用堆栈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM