Skip to content
全部文档

打包

Livewire 中每次组件更新都会触发网络请求。默认情况下,当多个组件同时触发更新时,它们会被打包到单个请求中。

这样可以减少与服务器的网络连接,并大幅降低服务器负载。

除了性能收益外,这也在内部解锁了需要多个组件协作的功能(响应式属性可绑定属性等)。

不过,有时出于性能考虑需要禁用这种打包。本页介绍在 Livewire 中自定义该行为的多种方式。

隔离组件请求

使用 Livewire 的 #[Isolate] 类属性,可将组件标记为「隔离」。这意味着每当该组件与服务器往返时,都会尝试与其他组件请求隔离开。

若更新代价较高、希望该组件的更新与其他组件并行执行,这会很有用。例如,页面上多个组件使用 wire:poll 或监听事件时,你可能希望隔离那些更新昂贵、否则会拖慢整个请求的特定组件。

php
<?php // resources/views/components/post/⚡show.blade.php

use Livewire\Attributes\Isolate;
use Livewire\Component;

new #[Isolate] class extends Component { // [tl! highlight]
    // ...
};

加上 #[Isolate] 属性后,该组件的请求将不再与其他组件更新打包在一起。

懒加载组件默认已隔离

当同一页面上许多组件通过 #[Lazy] 属性「懒」加载时,通常希望它们的请求彼此隔离并并行发送。因此 Livewire 默认会隔离懒加载更新。

若要禁用此行为,可向 #[Lazy] 属性传入 isolate: false 参数,例如:

php
<?php // resources/views/components/⚡revenue.blade.php

use Livewire\Attributes\Lazy;
use Livewire\Component;

new #[Lazy(isolate: false)] class extends Component { // [tl! highlight]
    // ...
};

现在,若同一页面上有多个 Revenue 组件,全部更新会打包成单个懒加载网络请求发送到服务器。