livewire-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLivewire Development
Livewire开发
When to Apply
适用场景
Activate this skill when:
- Creating or modifying Livewire components
- Using wire: directives (model, click, loading, sort, intersect)
- Implementing islands or async actions
- Writing Livewire component tests
在以下场景中使用本技能:
- 创建或修改Livewire组件
- 使用wire:指令(model、click、loading、sort、intersect)
- 实现独立组件(islands)或异步操作
- 编写Livewire组件测试
Documentation
文档参考
Use for detailed Livewire 4 patterns and documentation.
search-docs使用获取Livewire 4的详细模式和文档。
search-docsBasic Usage
基础用法
Creating Components
创建组件
<code-snippet name="Component Creation Commands" lang="bash">
<code-snippet name="Component Creation Commands" lang="bash">
Single-file component (default in v4)
Single-file component (default in v4)
{{ $assist->artisanCommand('make:livewire create-post') }}
{{ $assist->artisanCommand('make:livewire create-post') }}
Multi-file component
Multi-file component
{{ $assist->artisanCommand('make:livewire create-post --mfc') }}
{{ $assist->artisanCommand('make:livewire create-post --mfc') }}
Class-based component (v3 style)
Class-based component (v3 style)
{{ $assist->artisanCommand('make:livewire create-post --class') }}
{{ $assist->artisanCommand('make:livewire create-post --class') }}
With namespace
With namespace
{{ $assist->artisanCommand('make:livewire Posts/CreatePost') }}
</code-snippet>{{ $assist->artisanCommand('make:livewire Posts/CreatePost') }}
</code-snippet>Converting Between Formats
格式转换
Use to convert between single-file, multi-file, and class-based formats.
php artisan livewire:convert create-post使用在单文件、多文件和基于类的格式之间转换。
php artisan livewire:convert create-postComponent Format Reference
组件格式参考
| Format | Flag | Structure |
|---|---|---|
| Single-file (SFC) | default | PHP + Blade in one file |
| Multi-file (MFC) | | Separate PHP class, Blade, JS, tests |
| Class-based | | Traditional v3 style class |
| View-based | ⚡ prefix | Blade-only with functional state |
| 格式 | 标识 | 结构 |
|---|---|---|
| 单文件组件(SFC) | 默认 | PHP + Blade 单文件 |
| 多文件组件(MFC) | | 独立的PHP类、Blade视图、JS、测试文件 |
| 基于类的组件 | | 传统v3风格类 |
| 基于视图的组件 | ⚡前缀 | 仅Blade文件,带函数式状态 |
Single-File Component Example
单文件组件示例
<code-snippet name="Single-File Component Example" lang="php">
<?php
use Livewire\Component;
new class extends Component {
public int $count = 0;
public function increment(): void
{
$this->count++;
}
}
?>
<div>
<button wire:click="increment">Count: @{{ $count }}</button>
</div>
</code-snippet>
<code-snippet name="Single-File Component Example" lang="php">
<?php
use Livewire\Component;
new class extends Component {
public int $count = 0;
public function increment(): void
{
$this->count++;
}
}
?>
<div>
<button wire:click="increment">Count: @{{ $count }}</button>
</div>
</code-snippet>
Livewire 4 Specifics
Livewire 4特性
Key Changes From Livewire 3
与Livewire 3的主要差异
These things changed in Livewire 4, but may not have been updated in this application. Verify this application's setup to ensure you follow existing conventions.
- Use for full-page components; config keys renamed:
Route::livewire()→layout,component_layout→lazy_placeholder.component_placeholder - now ignores child events by default (use
wire:modelfor old behavior);wire:model.deeprenamed towire:scroll.wire:navigate:scroll - Component tags must be properly closed; now uses View Transitions API (modifiers removed).
wire:transition - JavaScript: →
$wire.$js('name', fn);$wire.$js.name = fn/commithooks →request/interceptMessage().interceptRequest()
这些是Livewire 4中的变更,但本应用可能尚未更新。请核实应用的设置以确保遵循现有约定。
- 对整页组件使用;配置项重命名:
Route::livewire()→layout,component_layout→lazy_placeholder。component_placeholder - 默认忽略子元素事件(如需旧行为请使用
wire:model);wire:model.deep重命名为wire:scroll。wire:navigate:scroll - 组件标签必须正确闭合;现在使用View Transitions API(已移除修饰符)。
wire:transition - JavaScript:→
$wire.$js('name', fn);$wire.$js.name = fn/commit钩子 →request/interceptMessage()。interceptRequest()
New Features
新特性
- Component formats: single-file (SFC), multi-file (MFC), view-based components.
- Islands () for isolated updates; async actions (
@island,wire:click.async) for parallel execution.#[Async] - Deferred/bundled loading: ,
deferfor optimized component loading.lazy.bundle
| Feature | Usage | Purpose |
|---|---|---|
| Islands | | Isolated update regions |
| Async | | Non-blocking actions |
| Deferred | | Load after page render |
| Bundled | | Load multiple together |
- 组件格式:单文件(SFC)、多文件(MFC)、基于视图的组件。
- 独立组件()用于隔离更新;异步操作(
@island、wire:click.async)用于并行执行。#[Async] - 延迟/捆绑加载:、
defer用于优化组件加载。lazy.bundle
| 特性 | 用法 | 用途 |
|---|---|---|
| 独立组件 | | 隔离更新区域 |
| 异步操作 | | 非阻塞操作 |
| 延迟加载 | | 页面渲染后加载 |
| 捆绑加载 | | 批量加载多个组件 |
New Directives
新指令
- ,
wire:sort,wire:intersect,wire:ref,.renderlessare available for use..preserve-scroll - attribute automatically added to elements triggering network requests.
data-loading
| Directive | Purpose |
|---|---|
| Drag-and-drop sorting |
| Viewport intersection detection |
| Element references for JS |
| Component without rendering |
| Preserve scroll position |
- 、
wire:sort、wire:intersect、wire:ref、.renderless已可用。.preserve-scroll - 触发网络请求的元素会自动添加属性。
data-loading
| 指令 | 用途 |
|---|---|
| 拖拽排序 |
| 视口交叉检测 |
| 供JS使用的元素引用 |
| 无渲染组件 |
| 保留滚动位置 |
Best Practices
最佳实践
- Always use in loops
wire:key - Use for loading states
wire:loading - Use for instant updates (default is debounced)
wire:model.live - Validate and authorize in actions (treat like HTTP requests)
- 循环中始终使用
wire:key - 使用实现加载状态
wire:loading - 使用实现即时更新(默认是防抖更新)
wire:model.live - 在操作中进行验证和授权(视为HTTP请求处理)
Configuration
配置
- defaults to
smart_wire_keys; new configs:true,component_locations,component_namespaces,make_command.csp_safe
- 默认值为
smart_wire_keys;新增配置项:true、component_locations、component_namespaces、make_command。csp_safe
Alpine & JavaScript
Alpine与JavaScript
- uses browser View Transitions API;
wire:transitionand$errorsmagic properties available.$intercept - Non-blocking and parallel
wire:pollupdates improve performance.wire:model.live
For interceptors and hooks, see reference/javascript-hooks.md.
- 使用浏览器的View Transitions API;
wire:transition和$errors魔术属性可用。$intercept - 非阻塞的和并行的
wire:poll更新提升性能。wire:model.live
有关拦截器和钩子,请查看reference/javascript-hooks.md。
Testing
测试
<code-snippet name="Testing Example" lang="php">
Livewire::test(Counter::class)
->assertSet('count', 0)
->call('increment')
->assertSet('count', 1);
</code-snippet><code-snippet name="Testing Example" lang="php">
Livewire::test(Counter::class)
->assertSet('count', 0)
->call('increment')
->assertSet('count', 1);
</code-snippet>Verification
验证
- Browser console: Check for JS errors
- Network tab: Verify Livewire requests return 200
- Ensure on all
wire:keyloops@foreach
- 浏览器控制台:检查JS错误
- 网络面板:验证Livewire请求返回200状态码
- 确保所有循环都使用
@foreachwire:key
Common Pitfalls
常见误区
- Missing in loops → unexpected re-rendering
wire:key - Expecting real-time → use
wire:modelwire:model.live - Unclosed component tags → syntax errors in v4
- Using deprecated config keys or JS hooks
- Including Alpine.js separately (already bundled in Livewire 4)
- 循环中缺失→ 意外重渲染
wire:key - 期望实时更新 → 使用
wire:modelwire:model.live - 未闭合的组件标签 → v4中会导致语法错误
- 使用已弃用的配置项或JS钩子
- 单独引入Alpine.js(Livewire 4已内置)