使用 blade 调用动态内容 (Livewire)

Calling dynamic content with blade (Livewire)

我正在项目中使用 laravel-modules。我还在项目中添加了 laravel-modules-livewire,我正在使用它来调用仪表板中的动态内容以允许配置每个模块。按照我的设计方式,每个模块都会有一个配置 livewire 组件,应该在帐户设置部分调用该组件,以允许每个客户端配置模块的行为方式。

在我的 Account/Modules livewire 中,我在挂载方法中有这个:

    public $client;
    public $modules;

    public function mount( Client $client )
    {
        $this->client  = $client;
        $this->modules = $client->getModules();
    }

$client->getModules 检索客户端激活的 collection 个模块。根据nWidart/Laravel-Modules,可以用$module->getName()得到模块的名字,也可以用$module->getLowerName()得到小写的名字。根据 mhmiton/laravel-modules-livewire,您应该使用 <livewire:{module-lower-name}::component-class-kebab-case /> 在模块内渲染您的 livewire 组件。所以我在 account/modules blade 中写了以下内容:

        @foreach($modules as $module)
            <livewire:{{ $module->getName() }}::configuration :client="$client" />
        @endforeach

我也试过了

        @foreach($modules as $module)
            <livewire:{{ $module->getLowerName() }}::configuration :client="$client" />
        @endforeach

但是其中 none 正在获取我的配置组件的渲染。最后我不得不将我的代码切换到替代符号

        @foreach($modules as $module)
            @livewire( $module->getLowerName() . '::configuration', [ 'client' => $client ] )
        @endforeach

这次渲染正确。我不想使用这种表示法,有人可以向我解释错误在哪里吗?如何使用第一个符号渲染它们?

可以通过Livewire的动态组件来实现。但是两者在功能上是等价的,所以使用任何一个应该都没有问题。

在我们深入研究动态组件之前,建议您使用 wire:key 键控每个动态组件,这应该是独一无二的。

添加了密钥的您的工作解决方案如下所示

@livewire($module->getLowerName() . '::configuration', ['client' => $client], key($loop->index))

如果你想使用 <livewire 标签,你可以使用 is 组件,

<livewire:is :component="$module->getLowerName() . '::configuration'" :client="$client" :wire:key="$loop->index" />