微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Laravel 5 js被app.js阻止

我在home.blade.PHP中使用了一个javascript,它扩展了layout / app.blade.PHP

@extend(layout.app)

当我在home.blade.PHP中扩展javascript停止工作时,我注释掉了

< script src =“{{asset('js / app.js')}}”defer>< / script>在app.blade.PHP然后javascript开始工作.我不确定有什么问题可以帮助我解决这个问题.

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

<!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>

解决方法:

您的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>

以上是您的主要布局文件.如果要在其他刀片文件中扩展它,您希望使用该布局文件添加内容或其他JavaScript功能.

为了以一种很好的方式实现这一点,我们使用@yield(‘javascript’)关键字来添加javascript文件函数.

因此,扩展此刀片文件的其他刀片文件应如下所示:

@extends('layouts.app')

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

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

首先,您要加载布局刀片文件,然后您可以将特定内容和javascript插入到使用关键字yield(‘xxx’)的每个部分

看了你的源代码后:

你正在home.blade.PHP中加载jquery这是不理想的,如果你想加载它你应该加载到你的主刀片PHP文件中.但是如果你使用laravels认的app.js文件,你实际上不需要加载它.认的app.js文件已经需要以下require(‘./ bootstrap’);.如果你没有改变它,那个文件已经包含了jquery.

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

} catch (e) {}

因此,请检查您的app.js是否需要引导程序文件,如果是,则检查引导程序文件是否包含jquery,您不需要在刀片文件中加载jquery.

因此,您应该在主刀片中加载app.js文件.在底部加载它:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐