Laravel 6 - @push 和 @stack

我是 Laravel 新手,目前正在使用 Laravel 6.0


出于某种原因,我使用 @push 的 javascript 无法正常工作。该脚本仅在我将代码粘贴到刀片文件中而不使用资产文件夹时才有效。


add.blade.php


@extends('layouts.app')

@section('content')

// Long HTML code

@endsection


@push('scripts')

<script src="{{ asset('js/position.js') }}"></script>

@endpush

布局 > app.blade.php


 <main class="py-4">

      @yield('content')

    </main>

  </div>

  @stack('scripts')

</body>

公共(资产)> js > position.js


 $('#department').change(function(){

    var departmentID = $(this).val();    


        if(departmentID) {

        $.ajax({

        type:"GET",

        url:"{{url('get-section-list-position')}}?department_id="+departmentID,

        success:function(res){               

            if(res) {

            $("#section").empty();

            $("#section").append('<option>Please Select Section</option>');

            $.each(res,function(key, value){

            $("#section").append('<option value="'+key+'">'+value+'</option>');

          });

          } else {

            $("#section").empty();

          }

        }

      });

    } else {

        $("#section").empty();

    }      

  });

如果我将脚本粘贴到 add.blade.php 并使用@section,它可以工作,但我担心我的系统的安全性会受到损害。请帮忙。


婷婷同学_
浏览 413回答 1
1回答

慕码人2483693

你的 Javascript 不能独立工作,因为它不会被 Blade 解析,但它包含{{url('get-section-list-position')}}. 如果您想使用 Blade 指令,请更改{{url('get-section-list-position')}}为“硬编码”网址或将您的 Javascript 代码放入您的内部:@push('scripts')@push('scripts')<script>$('#department').change(function(){&nbsp; &nbsp; var departmentID = $(this).val();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(departmentID) {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type:"GET",&nbsp; &nbsp; &nbsp; &nbsp; url:"{{url('get-section-list-position')}}?department_id="+departmentID,&nbsp; &nbsp; &nbsp; &nbsp; success:function(res){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(res) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#section").empty();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#section").append('<option>Please Select Section</option>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(res,function(key, value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#section").append('<option value="'+key+'">'+value+'</option>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#section").empty();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $("#section").empty();&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; });</script>@endpush
打开App,查看更多内容
随时随地看视频慕课网APP