Laravel 按需加载写法:
有时候我们只在单一页面需要引入某一JS文件或者CSS文件,我之前都是写在布局文件中,让他所有页面加载出来,这样的坏处是不需要加载的页面,冗余加载,影响加载速度,所以我们要按需加载。
在layout/app.balde.php文件中这样写:
<!-- Scripts --> <script src="{{ asset('js/app.js') }}"></script> @yield('scripts')
然后我们需要引入js文件的页面加入
@section('scripts')
<script src="https://cdn.bootcss.com/jquery/3.3.1/core.js"></script>
<script type="text/javascript" src="{{ asset('js/jquery.atwho.min.js') }}"></script>
@endsection
就可以了,这样只有此页面引入可此js文件。
同理引入CSS:
layout中写入:
<!-- Styles --> <link href="/css/app.css" rel="stylesheet"> @yield('styles')
页面引入:
@section('styles') <style> .atwho-view { position:absolute; top: 0; left: 0; display: none; margin-top: 18px; background: white; color: black; border: 1px solid #DDD; border-radius: 3px; box-shadow: 0 0 5px rgba(0,0,0,0.1); min-width: 120px; z-index: 11110 !important; } </style> @endsection
或者
@section('styles') <link rel="stylesheet" type="text/css" href="{{ asset('css/simditor.css') }}"> @stop
Comments (0)