php - Laravel 中为不同 View 加载不同的 css/js

标签 php css laravel

我是 Laravel 新手。我使用 Laravel 5.6 。我想仅在一页( View )中使用 Summernote WYSIWYG 编辑器。 Summernote 需要一个 css 和一个 js 文件。我只想在此 View 中加载这些文件。我该怎么做?
这是我尝试这样做的方法。
ma​​ster.blade.php 文件。

<html>

    @include('header')

    <body>

        @yield('content')

        @include('footer')

    </body>
</html>

editor.blade.php文件

@extends('master')

    @section('content')
        --- Summernote editor goes here---
    @endsection

    @section('imports')
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script>

    <!-- include summernote css/js-->
    <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>

    <script>
        $(document).ready(function() {
            $('.summernote').summernote();
        });
    </script>
    @endsection

header.blade.php

<head>
    --- other css and file links ---

    @yield('imports')
</head>

如您所见,editor.blade.php 文件扩展了 master.blade 文件。在主文件中,我包含了 header.blade 文件,其中包含所有 css 链接。所以我在 header.blade 文件中生成了 Summernote js/css。但是当它加载到浏览器时, header.blade 文件中生成的内容位于 <body> 的开头。标签(应位于 <head> 标签内)。
我可以直接将这些文件添加到 headr.blade.php 文件中。但我想知道是否有办法以这种方式做到这一点。

最佳答案

我通常以这种方式执行此操作,您不会在 master.blade 中使用 include() Blade ,因为无论如何它都会调用所有页面,只需让出,然后在其他页面中注入(inject)到该生成的部分。

master.blade.js

<html>

    @yield('header')

    <body>

        @yield('content')

        @yield('footer')

    </body>
</html>

header.blade.php

 @extends('master')

    @section('header')
            --- CSS here ---
    @endsection

    @section('content')
        --- content here ---
    @endsection

    @section('footer')
        --- scripts here ---
    @endsection

关于php - Laravel 中为不同 View 加载不同的 css/js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49395734/

相关文章:

php - 如何在具有多个数据库的 Laravel 5.7 应用程序中从替代数据库获取特定数据

php - 如何在 Laravel 5.2 中测试文件上传

php - 对列的值使用分组依据和条件

php - 检索从特定行开始的数据库条目

php - 替换模式内的所有事件

php - 如何在 CRUD 应用程序中更 Laravel?

php - 将 var 从 php 传递到 javascript

html - 为什么 flexbox 在包装元素后不收缩以适应?

css - Bootstrap 自定义导航栏形状

CSS 过渡时间问题