You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Livewire组件的用户数据传递给Blade组件?

解决方法

1. 修正Livewire组件的属性定义

先补全属性的public修饰符和$符号(你之前的代码遗漏了):

public $dataAlumni;
public $showingUserProfileCard = false;

public function showUserProfileCard(User $alumni){
    $this->dataAlumni = $alumni;
    $this->showingUserProfileCard = true;
}

2. 传递Livewire组件中的用户数据到ProfileCard

在模态框内的<x-profile-card>标签中,直接绑定Livewire组件的$dataAlumni属性,不要使用循环里的$alumni:

<x-modal maxWidth="fit" wire:model="showingUserProfileCard">
    <x-profile-card :user="$dataAlumni" />
</x-modal>

3. 确保ProfileCard组件正确接收属性

如果是类组件,在组件类中定义$user属性:

// app/View/Components/ProfileCard.php
namespace App\View\Components;

use App\Models\User;
use Illuminate\View\Component;

class ProfileCard extends Component
{
    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function render()
    {
        return view('components.profile-card');
    }
}

如果是匿名组件,在组件的Blade文件开头声明user属性:

<!-- resources/views/components/profile-card.blade.php -->
@props(['user'])

<!-- 组件内容示例 -->
<div class="profile-card">
    <h2>{{ $user->name }}</h2>
    <p>{{ $user->email }}</p>
    <!-- 其他用户信息字段 -->
</div>

4. 可选:关闭模态框时重置数据

为避免关闭模态框后残留旧数据,可在Livewire组件中添加更新钩子:

public function updatedShowingUserProfileCard($isOpen)
{
    if (!$isOpen) {
        $this->dataAlumni = null;
    }
}

内容的提问来源于stack exchange,提问作者Zulfiqry Saadputra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 21:47:37