【问题标题】:Laravel livewire data disappearingLaravel livewire 数据消失
【发布时间】:2021-05-14 18:54:54
【问题描述】:

我有非常简单的 livewire 渲染功能,但数据在一秒钟后从刀片中消失。

component

public function render()
{
  $spotlight = Project::orderby('created_at', 'desc')->where('published', '=', 'y')->with('user')->offset(5)->take(5)->latest()->get();
  return view('livewire.index.job-spotlight', compact('spotlight'));
}

view

<div>
    <h3 class="margin-bottom-5">Panel name</h3>

    <div id="job-spotlight" class="showbiz-container">
        <div class="showbiz" data-left="#showbiz_left_1" data-right="#showbiz_right_1" data-play="#showbiz_play_1" >
            <div class="overflowholder">
                <ul>
                    @foreach($spotlight as $spots)
                    <li wire:poll.keep-alive>
                    {{$spots->title}}
                    </li>
                    @endforeach
                </ul>
            </div>
        </div>
    </div>
</div>

注意::&lt;li&gt; 标签上有一个wire:poll.keep-alive

有什么想法吗?

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    在关注文档时,您会看到他们建议使用 mount 功能,因此请尝试类似的操作。我知道这样的问题可能看起来有点太多了,但对于未来的使用,它会让事情变得更加“可读”..

    component
    
    public $spotlight;
    
    public function mount(Spotlight $spotlight) *or it can be ()*
    {
        $this->spotlight = Project::orderby('created_at', 'desc')->where('published', '=', 'y')->with('user')->offset(5)->take(5)->latest()->get();
    }
    
    public function render()
    {
        return view('livewire.index.job-spotlight', [
            'spotlight' => $this->spotlight,
        ]);
    }
    

    这应该可以工作,因为您正在正确地创建和设置变量,我不认为&lt;li&gt; 标签上的wire:poll.keep-alive 真的有什么不同。让我知道这是否有帮助,我再次知道它有很多代码用于一个小功能,但对于刀片的任何未来添加,它都会让事情变得更容易。

    【讨论】:

      猜你喜欢
      • 2023-01-16
      • 2021-05-04
      • 2021-06-30
      • 1970-01-01
      • 2021-09-23
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      相关资源
      最近更新 更多