Livewire 在尝试冻结 [data-table] 组件时遇到损坏的数据,当尝试将闭包发送到 livewire 组件时

Livewire encountered corrupt data when trying to hydrate the [data-table] component, when trying to send closure to livewire component

提问人:Irfansyah Rizal 提问时间:10/13/2022 最后编辑:Mustafa PoyaIrfansyah Rizal 更新时间:10/13/2022 访问量:224

问:

我试图向嵌套数组内的 livewire 组件发送一些闭包,但它产生了以下错误:

Livewire 在尝试冻结 [data-table] 组件时遇到损坏的数据。确保 Livewire 组件的 [name, id, data] 在请求之间未被篡改。

我发送到组件的内容:


<livewire:data-table
    :model="$modelClass"
    :custom="[
        [
            'label' => 'E-Mail',
            'column'=> 'email'
        ],
    ]"
    :exclude="['password', 'email_verified_at', 'remember_token', 'updated_at']"
    :include="[
        [
            'label' => 'Role',
            'column'=> 'role.name',
            'format'=> fn($value, $row) => '<strong>'.ucwords($value).'</strong>',
            'formatType' => 'html'
        ],
        [
            'label' => 'Search Engine',
            'links' => [
                [
                    'title' => fn($row) => $row->name.' Google',
                    'link'  => fn($row) => 'https://google.com/search?q='.$row->name,
                    'type'  => 'button',
                ]
            ],
        ],
        [
            'label' => 'Social Media',
            'links' => [
                [
                    'title' => 'Facebook',
                    'link'  => 'https://facebook.com',
                    'type'  => 'link'
                ],
                [
                    'title' => 'Instagram',
                    'link'  => 'https://instagram.com',
                    'type'  => 'link'
                ]
            ]
        ],
        [
            'label' => 'Email Provider',
            'links' => [
                [
                    'title' => function($row){
                        if(str_contains($row->email, 'gmail')){
                            return 'Google';
                        }elseif(str_contains($row->email, 'yahoo')){
                            return 'Yahoo';
                        }else{
                            return 'Unidentified';
                        }
                    },
                    'class' => 'cursor-pointer',
                    'onclick'  => 'return false',
                ]
            ]
        ]
    ]"
/>

然后我签入 ComponentCheckshumManager 类,当使用 json_encode 进行哈希处理时,包含闭包的数组变为空。

哈希前:哈希前的数据

哈希后:哈希后的数据

这个错误只在刷新组件时发生,有没有正确的方法可以将闭包发送到 livewire 组件?请帮忙。

PHP 数组 Laravel 闭包 laravel-livewire

评论


答:

0赞 raimondsL 10/13/2022 #1

取决于。如果它是一个属性,那么问题就在其他地方,如果它是一个魔术函数,那么你就有问题了。每次模板修改原始数据时,您都会得到损坏的数据。即使它是神奇的功能,它也会破坏原始数据。name$user->full_name

评论

0赞 Irfansyah Rizal 10/13/2022
我不完全理解,但我认为这是一种属性。错误仅在属性中发生。在 livewire 组件中,使用 public 属性接收的参数以及当组件刷新时会发生上述错误。但是当我使用受保护的属性和挂载函数来接收参数时,上面的错误没有发生,当组件刷新时,数据变成了一个空数组。:include