我是 Laravel 新手。我使用 Laravel 5.6 。我想仅在一页( View )中使用 Summernote WYSIWYG 编辑器。 Summernote 需要一个 css 和一个 js 文件。我只想在此 View 中加载这些文件。我该怎么做?
这是我尝试这样做的方法。
master.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/