【问题标题】:laravel View Componentslaravel 查看组件
【发布时间】:2021-09-05 07:28:51
【问题描述】:

我有一个视图组件,它位于 components 文件夹中,但位于名为 models 的子文件夹中 我一直使用以下标签访问它

<x-models.models></x-models.models>

它一直运行良好,但现在我需要将数据传递给它。 我了解App/View/Components 的工作原理,但由于刀片位于子文件夹中,我无法弄清楚如何创建组件。 有什么想法吗?

【问题讨论】:

  • 你需要没有组件类吗?
  • 不,我需要一个组件类
  • 好的。你仍然可以在没有类的情况下将数据传递给 x 组件。最好发布 x 模型组件代码以及你想要传递给它的数据。所以有人可以帮助你解决你的问题
  • 如果组件不在子文件夹中,那么很容易。我创建一个 App\View\Components\Models.php 并从那里传递 $models 。问题是models.blade.php 不是resources\views\components。它位于资源\视图\组件\模型中。
  • 即使你可以在没有类的情况下将数据传递给子组件

标签: laravel view components


【解决方案1】:

使用类可以返回子组件视图

<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Model extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.models.models');


    }
}

同样没有类,我们也可以将数据传递给子组件模型

例如,我在views/components/bootstrap/modal.blade.php 中创建了一个模型

@props(['title' => '','footer'=>'','id'=>''])

    <div {{ $attributes->merge(['class' => 'modal fade commonModal']) }}  id="{{$id}}" tabindex="-1" role="dialog"    aria-labelledby="{{$id}}" aria-hidden="true"  >
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">{{$title}}</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body ">
                  {{$slot}}
                </div>
                <div class="modal-footer">
                 {{$footer}}
    
                </div>
            </div>
        </div>
    </div>

访问时

 <x-bootstrap.modal id="modalId" class="modelClass">
        <x-slot name="title">
           Title
        </x-slot>
       
     whaterve content
    </x-bootstrap.modal>

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 2016-08-09
    • 2017-08-25
    • 2018-06-24
    • 1970-01-01
    • 2013-06-19
    • 2021-11-19
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多