首页 > 解决方案 > 有没有办法使用 @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 文件?

标签: csslaravel

解决方案


我意识到要让@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 文件,我能够以任何方式对其进行排序。


推荐阅读