laraspells / asset-collector
Blade 指令用于防止多次插入多个资产(CSS/JS)代码。
dev-master
2017-09-22 14:08 UTC
This package is auto-updated.
Last update: 2024-08-29 04:54:20 UTC
README
Asset collector 是一个库,包含一组 Blade 指令,用于防止多次插入多个资产(CSS/JS)代码。Asset collector 由 LaraSpell 用于需要一些 CSS 和 JS 代码的字段组件。
例如,如果你有一个视图部分 input-select2.blade.php
如下所示
<select name="{{ $name }}" class="form-control use-select2" id="{{ $id }}"> @foreach($options as $option) <option value="{{ $option['value'] }}">{{ $option['label'] }}</option> @endforeach </select> @push('styles') <link rel="stylesheet" href="{{ asset('path/to/select2/select2.min.css') }}"> @endpush @push('scripts') <script src="{{ asset('path/to/select2/select2.min.js') }}"></script> <script> $(function() { $("select.use-select2").select2(); }) </script> @endpush
如果你两次包含(使用 @include
)该文件,链接和脚本代码也将渲染两次。这会增加渲染的页面大小,可能会对某些插件造成麻烦。
这个库正是为了防止这种情况。使用 asset collector,上述代码将变为如下所示
<select name="{{ $name }}" class="form-control use-select2" id="{{ $id }}"> @foreach($options as $option) <option value="{{ $option['value'] }}">{{ $option['label'] }}</option> @endforeach </select> @css('path/to/select2/select2.min.css') @js('path/to/select2/select2.min.js') @script <script> $(function() { $("select.use-select2").select2(); }) </script> @endscript
然后在主视图中,你可以这样输出收集到的资产
<!doctype html> <html> <head> ... @section('styles') @styles() @show ... </head> <body> ... @section('scripts') @scripts() @show </body> </html>
安装
运行以下 composer 命令来安装 asset-collector
composer require "laraspells/asset-collector"
然后打开你的 config/app.php
文件,将 LaraSpells\AssetCollectorServiceProvider::class
添加到 'providers' 部分
就这样!