【问题标题】:How can I include js files correspondes to render files in laravel blade template?如何在 laravel 刀片模板中包含对应于渲染文件的 js 文件?
【发布时间】:2019-02-04 09:35:03
【问题描述】:

在我的 laravel 应用程序中,我有 app.blade.php 和以下代码

<html>
    <body>
        <div>
            @yield('content')
        </div>
        <script src="{{ asset('jquery.min.js') }}" ></script>
        <script src="{{ asset('bootstrap.min.js') }}" ></script>
    </body>
</html>

我有几个页面,例如 page1page2 等,每个页面都呈现为 page1.blade.php 等,这些页面有单独的 javascript 文件,例如 page1.jspage2.js

page1.js 应仅包含在 page1.blade.php 中,page2.js 也应包含在内。如何为相应的刀片文件包含 js 文件?

我的page1.blade.php

@extends('layouts.app')

@section('content')
<section class="content-header">
  <h1>
    Page 1
  </h1>

</section>
@endsection

【问题讨论】:

    标签: javascript laravel-5 laravel-blade


    【解决方案1】:

    app.blade.php

    <html>
        <body>
            <div>
                @yield('content')
            </div>
            <script src="{{ asset('jquery.min.js') }}" ></script>
            <script src="{{ asset('bootstrap.min.js') }}" ></script>
            @yield('script')
        </body>
    </html>
    

    page1.blade.php

    @extends('layouts.app')
    
    @section('content')
    <section class="content-header">
      <h1>
        Page 1
      </h1>
    
    </section>
    @endsection
    
    @section('script')
      <script src="{{ asset('page1.js') }}" ></script>
    @endsection
    

    【讨论】:

      【解决方案2】:

      您可以使用 5.1 版本之后的 @stack 方法。 详情:https://laravel.com/docs/5.7/blade#stacks

      【讨论】:

        猜你喜欢
        • 2020-11-21
        • 2013-10-13
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2018-10-03
        • 1970-01-01
        • 2017-01-02
        • 2013-11-22
        相关资源
        最近更新 更多