css - 有没有办法使用 @push 和 @stack 进行复杂的 laravel 刀片布局?
问题描述
使用刀片我有一个布局,我创建了一个包含多个视图刀片组件的包,然后这些组件在应用程序刀片上呈现,如下所示。
布局 > 应用
<head>
@stack('css')
@yield('styles')
</head>
<body>
<main class="py-4">
@yield('content')
{{--Render package components--}}
@includeIf('package::view-blade')
</main>
@yield('scripts')
</body>
我能够从组件中渲染脚本,如下所示:
包 > 资源 > 组件 > 视图刀片
@section('scripts')
@parent
<script src="{{asset('path-to-scripts')}}"></script>
@endsection
但是当我尝试相同的样式时它不起作用,即
@section('styles')
@parent
<link type="text/css" href="{{asset('path-to-styles')}}"/>
@endsection
或者
@push('css')
<link type="text/css" href="{{asset('path-to-styles')}}"/>
@endpush
如何从包组件文件动态地将样式渲染到 app.blade 文件?
解决方案
我意识到要让@push 工作,文件必须使用@extends。因为我将组件直接渲染到布局 app.blade 中,这意味着它无法工作。本质上我将文件设置为
<head>
@stack('css')
@yield('styles')
</head>
<body>
<main class="py-4">
@yield('content')
{{--Render package components--}}
@section('styles')
@parent
<link type="text/css" href="{{asset('path-to-styles')}}"/>
@endsection
</main>
@yield('scripts')
</body>
我不明白为什么脚本有效但样式无效。
通过将代码 @includeIf('package::view-blade') 移动到扩展应用程序布局的单独的welcome.blade 文件,我能够以任何方式对其进行排序。
推荐阅读
- django - Django REST Serializer 使用错误的模型进行序列化
- python - python文件写入程序运行时如何更新桌面上的文件大小
- javascript - 使用 d3.js 更新表数据
- c# - C#捕获从不在进程中的函数返回的异常?
- r - 如何设置仅在输入 3 时才显示集合向量的函数?
- javascript - 如果 URI 没有改变,例如在单页应用程序上,如何检测用户是否在新页面上?
- angular - Angular Kendo UI 全局访问
- php - 内连接循环通过
- git - 如何 git rebase 从另一个分支直接到 master 分支?
- javascript - 受控数字比例映射