9

我收到以下错误,对此有点迷失:

Livewire 在尝试对 ... 组件进行水合时遇到损坏的数据。确保 Livewire 组件的 [name, id, data] 在请求之间没有被篡改

情况如下:Livewire 2.x,Laravel 7.x,组件控制器从 3 个 MySQL 存储过程中获取数据并进行处理。组件刀片是一个非常基本的刀片,带有 foreach 循环。我正在使用 wire:init 功能,以便组件不会阻塞页面加载。它包含一个定制的分页。切换到第二页数据时,出现此错误。在 Livewire 1.x 上它没有出错。

有没有人知道如何解决这个问题?错误本身对我来说并没有多大意义。需要任何其他信息吗?

提前感谢您,感谢您的帮助!

4

11 回答 11

24

在我的情况下,解决方案是保护公共财产并手动将其传递给刀片,因此它被排除在 Livewire 引擎盖下的自动处理之外。

于 2020-09-14T12:38:43.110 回答
4

要解决此问题,请打开 vendor/livewire/livewire/src/ComponentChecksumManager.php 文件并var_dump($stringForHashing);在第 19 行添加,就在 return 语句之前。然后您可以看到正在被散列的数据并将其与之前的散列数据进行比较以找出差异。

完成此操作后,我能够识别 javascript 正在重新排列的数字键并提出适当的修复。

需要注意的一点是,一些 json 格式化程序也会重新排序数字键,因此最好在不格式化或手动格式化的情况下比较 json。

于 2021-08-18T16:58:24.683 回答
1

我运行以下命令然后它解决了

php工匠优化

于 2021-10-13T08:08:02.520 回答
0

就我而言,我有几个公共变量,并非所有变量都是从父视图传入的。阅读马库斯的答案后。我去将该mount方法添加到我的 livewire 类中,并传入我从父级继承的变量并且它起作用了。我正在使用 Livewire 2.3

这有错误

class MyView extends Component
{
  public $fromParent, $local;

  public function render()
  {
    return view('livewire.my-view');
  }
}

添加安装固定它

class MyView extends Component
{
  public $fromParent, $localVariable1, localVariable2;

  public function mount($fromParent)
  {
    $this->fromParent = $fromParent;
  }

  public function render()
  {
    return view('livewire.my-view');
  }
}
于 2021-02-16T06:13:19.993 回答
0

除了上面其他人的建议之外,如果您有一个使用groupBy()可能是导致此问题的原因的集合,也可能会发生这种情况,

protected $attribute在您的组件中修复此使用,而不是public传递$attribute给组件视图。

protected $attribute;

public function mount($attribute){
   $this->attribute = $attribute;
}

...........

public function render()
{
    return view('livewire.view-here',['attribute'=>$attribute]);
}
于 2021-07-27T07:49:35.363 回答
0

就我而言,我在另一个组件中有受影响的 livewire 组件。

//livewire component
<x-layouts.content-nosidebar title="false">
     <div class="verification section-padding">
     </div>
</x-layouts.content-nosidebar>

我将 livewire 组件删除到它自己的文件中,并且不再收到错误消息。

//livewire component
<div class="verification section-padding">
</div>
于 2022-02-02T09:48:09.237 回答
0

我这样做了,它对我有用:

php artisan config:cache
php artisan config:clear
于 2021-09-14T09:12:42.323 回答
0

就我而言,问题来自数据库查询和结果集合。

这个查询(变量的含义不重要)

DB::table('progress_trackings')
        ->select('user_id')
        ->whereIn('user_id', $users->pluck('id'))
        ->where('object_id', $activity->id)
        ->where('event', $type)
        ->get()
        ->keyBy('user_id');

产生了这个输出:

Illuminate\Support\Collection {#2427
 all: [
   454 => {#2279
     +"user_id": 454,
   },
   528 => {#441
     +"user_id": 528,
   },
   531 => {#2368
     +"user_id": 531,
   },
 ],

该查询由刀片文件中的按钮触发。单击按钮 2 或 3 次导致水合错误。

在 Livewire 文档中解释了数组键有时会导致问题:https ://laravel-livewire.com/docs/2.x/troubleshooting

所以我试图从 db 查询结果中创建一个普通数组。因为我只需要钥匙,所以这很有效:

DB::table('progress_trackings')
    ->select('user_id')
    ->whereIn('user_id', $users->pluck('id'))
    ->where('object_id', $activity->id)
    ->where('event', $type)
    ->get()
    ->keyBy('user_id')
    ->keys()->toArray(); // ADDED THIS

现在结果是:

[
 454,
 528,
 531,

]

这解决了水合错误。

于 2021-06-23T12:01:13.597 回答
0

在我的情况下,它是 API 的图像访问器,我试图在模型中获取 API 的完整链接图像,这是我的错误

public function getImageAttribute($value)
{ 
   return  $value ? url(Storage::url($value)) : ''; 
}

这导致我 Livewire 遇到损坏的数据..

我的解决方案是

 public function getImageAttribute($value)
{
    if (Str::contains(Request()->route()->getPrefix(), 'api')) {
        return  $value ? url(Storage::url($value)) : '';
    }
    return $value;
}
于 2022-01-12T22:30:29.367 回答
0

所以我得到了同样的错误,这个:

Livewire encountered corrupt data when trying to hydrate the [component] component. Ensure that the [name, id, data] of the Livewire component wasn't tampered with between requests.

在尝试了接受的答案并阅读了 Livewire 的 Github 上的这个长问题线程之后,我尝试以不同的方式将数据传递给视图。我在这里分享它是为了帮助其他人解决同样的用例问题。

我发现了以下更改,我之前传递的一个 url 我刚刚进入视图,使它消失了。从:

// Class
class MyComponent extends Component
{
    public Shop $shop;
    public Address $address;
    public string $action;

    // rules etc

    public function mount(Shop $shop)
    {
        if ($shop->address()->exists()) {
            $this->address = $shop->address;
            $this->form_action = route('merchant.shop.address.update');
        } else {
            $this->address = Address::make();
            $this->form_action = route('address.store');
        }
    }

// view
<div>
    <form method="post" action="{{ $action }}">
        @csrf

至:

// class
class MyComponent extends Component
{
    public Shop $shop;
    public Address $address;

    // rules etc

    public function mount(Shop $shop)
    {
        $this->address = $shop->address()->exists() ? $shop->address : Address::make();
    }

// view
<div>
    <form method="post" action="{{ $shop->address()->exists() ? route('merchant.shop.address.update') : route('address.store') }}">
于 2021-02-01T18:07:34.473 回答
-1

我通过将 livewire 脚本 @livewireScripts 放在页面标题中来解决它

<head>

<meta charset="utf-8" />

@livewireStyles

@livewireScripts

</head>
于 2021-03-31T10:07:26.423 回答