问题描述
我遇到以下错误,并对此感到迷茫:
Livewire在尝试水合……时遇到了损坏的数据。 零件。确保Livewire组件的[名称,ID,数据] 在两次请求之间没有被篡改
情况如下:Livewire 2.x,Laravel 7.x,组件控制器从3个MysqL存储过程中获取数据并对其进行处理。组件刀片是一个非常漂亮的基本刀片,带有foreach循环。我正在使用wire:init功能,以便该组件不会阻止页面加载。它包含一个定制的分页。切换到第二页数据时,会发生此错误。在Livewire 1.x上,它没有出错。
有人对如何解决这个问题有任何想法吗?错误本身对我来说并不多。还需要其他信息吗?
预先感谢您,感谢您的帮助!
解决方法
在我的情况下,解决方案是对公共财产进行保护,然后将其手动传递给刀片服务器,因此它不在Livewire的自动处理范围内。
,所以我得到了同样的错误,这个错误:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_signout -> {
FirebaseAuth.getInstance().signOut()
return true
}
R.id.search -> {
navController.navigate(R.id.nav_service_search)
if (fragmentManager.findFragmentById(R.id.userInfoFragment) != null) {
navController.popBackStack(R.id.userInfoFragment,false)
}
return true
}
R.id.user_account -> {
navController.navigate(R.id.userInfoFragment)
if (fragmentManager.findFragmentById(R.id.userInfoFragment) != null) {
navController.popBackStack(R.id.userInfoFragment,false)
}
return true
}
}
return super.onOptionsItemSelected(item)
}
在尝试接受的答案并阅读 Livewire 的 Github 上的 this long issue thread 后,我尝试以不同的方式将数据传递到视图。我在此分享它是为了帮助遇到相同用例问题的其他人。
我发现了以下更改,我之前传递的 url 刚刚进入视图,但它消失了。来自:
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.
到:
// 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
,
就我而言,我有几个公共变量,但并非所有变量都是从父视图传入的。阅读马库斯的回答后。我去将 mount
方法添加到我的 livewire 类并传入我从父级继承的变量并且它起作用了。我使用的是 Livewire 2.3
这里有错误
class MyView extends Component
{
public $fromParent,$local;
public function render()
{
return view('livewire.my-view');
}
}
添加 mount 修复它
class MyView extends Component
{
public $fromParent,$localVariable1,localVariable2;
public function mount($fromParent)
{
$this->fromParent = $fromParent;
}
public function render()
{
return view('livewire.my-view');
}
}
,
我通过将 livewire 脚本 @livewireScripts 放在页眉中解决了这个问题
<head>
<meta charset="utf-8" />
@livewireStyles
@livewireScripts
</head>
,
就我而言,问题来自数据库查询和结果集合。
这个查询(变量的含义不重要)
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,
]
解决了水合错误。
,除了上面其他人的建议之外,如果您有一个使用 groupBy()
的集合,也可能会发生这种情况,这可能是导致此问题的原因,
要解决此问题,请在组件中使用 protected $attribute
而不是 public
,然后将 $attribute
传递给组件视图。
protected $attribute;
public function mount($attribute){
$this->attribute = $attribute;
}
...........
public function render()
{
return view('livewire.view-here',['attribute'=>$attribute]);
}