【问题标题】:How to prevent using a parameter when before clicked in Livewire在 Livewire 中单击之前如何防止使用参数
【发布时间】:2021-04-06 20:00:19
【问题描述】:

此代码是与 Livewire 组件连接的视图文件的一部分。

如何防止在单击按钮之前使用参数?

查看

    ...
        @foreach ($babies as $baby)
    ...
                <div
                    class="flex justify-between w-11-12 py-1  mb-1 ml-2 leading-6 text-sm font-medium ext-left text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-blue-800 focus:outline-none">
                    <button wire:click="setAte({{ $baby, $ate = 0 }})"
                        class="flex w-1-3 mr-1 px-2 text-xs font-medium text-center text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-pink-800 focus:ring-ring-600 {{ $baby['ate'] == 0 ? "bg-blue-500" : "bg-red-500"   }}">
                        S</button>
                    <button wire:click="setAte({{ $baby, $ate = 1 }})"
                        class="flex w-1-3 mr-1 px-2 text-xs font-medium text-center text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-pink-800 focus:ring-pink-600 {{ $baby['ate'] == 1 ? "bg-blue-500" : "bg-red-500"}}">
                        M</button>
                    <button wire:click="setAte({{ $baby, $ate = 2}})"
                        class="flex w-1-3 px-2 text-xs font-medium text-center text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-pink-800 focus:ring-pink-600 {{ $baby['ate'] == 2 ? "bg-blue-500" : "bg-red-500"  }}">
                        L</button>
                    <div>{{ $baby['ate'] }}</div> {{-- <---check code --}}
                </div>
    @endforeach
    ...

组件

public function setAte($baby, $ate)
{
    $baby['ate'] = $ate;
    $selectedBaby = Baby::find($baby['id']);
    $selectedBaby->save();
}

错误

Illuminate\Contracts\Container\BindingResolutionException 无法解析 App\Http\Livewire\Classroom\BabyStatus 类中的依赖项 [Parameter #1 [ $ate ]]

解决方案 1

//组件

public $baby;

protected $rules = [
    'baby.ate' => 'required|int|min:0|max:2'
];

public function updateAte($ate)
{
    $this->baby->ate = $ate;
    $this->baby->save();
}

//查看

<button wire:model="baby" wire:click="updateAte(0)"
    class="flex w-1-3 mr-1 px-2 text-xs font-medium text-center text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-pink-800 focus:ring-ring-600 {{ $baby['ate'] == 0 ? "bg-blue-500" : "bg-red-500"}}">S</button>
<button wire:model="baby" wire:click="updateAte(1)"
    class="flex w-1-3 mr-1 px-2 text-xs font-medium text-center text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-pink-800 focus:ring-pink-600 {{ $baby['ate'] == 1 ? "bg-blue-500" : "bg-red-500"}}">M</button>
<button wire:model="baby" wire:click="updateAte(2)"
    class="flex w-1-3 px-2 text-xs font-medium text-center text-white uppercase transition rounded shadow ripple hover:shadow-lg hover:bg-pink-800 focus:ring-pink-600 {{ $baby['ate'] == 2 ? "bg-blue-500" : "bg-red-500"  }}">L</button>

就是这样,谢谢@Unflux。

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    我倾向于重构你所拥有的并将每个baby 单独设为livewire component

    App\Http\Livewire\Baby.php

    class Baby extends Component
    {
        // your instance of a baby
        public $baby;
    
        // required to allow updating of the baby properties
        protected $rules = [
            'baby.ate' => 'required|int|min:0|max:2'
        ];
    
        public function render()
        {
            return view('livewire.baby');
        }
    
        // you can set the value of properties here if you want
        public function mount()
        {
            // for example:
            //$this->baby->ate = -100;
        }
    
        // when the value of ate is updated from the view
        // update the baby model
        public function updatedBaby()
        {
            $this->baby->save();
        }
    }
    

    资源\视图\livewire\baby.blade.php

    <div class="p-4 m-4">
        <h4>Baby Component - ate: {{ $baby->ate }}</h4>
        <div class="flex justify-between p-4 m-4">
            <button wire:click="$set('baby.ate', 0)"
                    class="text-white px-2 rounded {{ $baby->ate === 0 ? "bg-blue-500" : "bg-red-500" }}">S
            </button>
            <button wire:click="$set('baby.ate', 1)"
                    class="text-white px-2 rounded {{ $baby->ate === 1 ? "bg-blue-500" : "bg-red-500" }}">M
            </button>
            <button wire:click="$set('baby.ate', 2)"
                    class="text-white px-2 rounded {{ $baby->ate === 2 ? "bg-blue-500" : "bg-red-500" }}">L
            </button>
        </div>
    </div>
    

    注意:以上使用===。防止 null 或类似值被错误地确定为等于 0

    然后在你的view:

    @foreach($babies as $baby)
      @livewire("baby", ["baby" => $baby]) // render your single baby component
    @endforeach
    

    【讨论】:

    • 谢谢,但无法在子组件中使用更新的$baby['ate' ] 重新渲染。
    • @JsWizard $baby是什么,是数组还是模型?
    • 现在它也可以在子组件中工作,但是出现了重新渲染问题的新问题。我认为更新的$baby 数据必须传递给父组件,对吧?
    • 如果您在父组件中使用更新的$baby 模型,那么是的,您需要通知父组件。如果您的父母只是循环遍历 $babies 集合而不做任何其他事情,我会删除它并只使用普通刀片 @foreach,如我的示例所示。
    • 我在更新方法的末尾使用return redirect()-&gt;to('/child-component'); 解决了这个问题,但现在它是一种优雅的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2015-11-28
    • 1970-01-01
    • 2017-07-19
    • 2021-01-28
    • 2023-01-26
    • 2017-09-24
    相关资源
    最近更新 更多