简体   繁体   English

Laravel-livewire:为什么触发事件会执行 render() 方法?

[英]Laravel-livewire: Why does firing an event execute the render() method?

This is the Livewire framework for Laravel这是 Laravel 的 Livewire 框架

HTML: HTML:

<button wire:click="$emit('postAdded')">

PHP: PHP:

protected $listeners = ['postAdded' => 'showPostAddedMessage'];

public function showPostAddedMessage()
{
    // Do stuff
}

public function render()
{
    return view('livewire.index');
}

Clicking the button calls showPostAddedMessage() and after that render().单击按钮调用 showPostAddedMessage() 并在此之后调用 render()。 How can I listen for an event without calling render()?如何在不调用 render() 的情况下监听事件?

I had the same issue.我遇到过同样的问题。 In my case, the render() method was closing my parent modal box.就我而言, render() 方法正在关闭我的父模式框。 I just added "wire:ignore" in modal div.我刚刚在模态 div 中添加了“wire:ignore”。 Livewire ignores that div whenever it calls the render() method每当调用 render() 方法时,Livewire 都会忽略该 div

This is how livewire works.这就是livewire的工作原理。 whenever you are changing anything / firing any event.每当您更改任何内容/触发任何事件时。 the component will refresh.组件将刷新。 As far as I learned livewire, there is no way to stop it unless you are putting die() isnide showPOstAddedMessage function, which will be a very wierd way to solve it.据我所知,没有办法阻止它,除非你把 die() iside showPOstAddedMessage function,这将是一个非常奇怪的解决方法。

I've came to this sort of hack: if your concern is to not re-render the view, you can just return an empty string from render() .我遇到了这种 hack:如果您担心不重新渲染视图,您可以从render()返回一个空字符串。 The DOM will not be updated. DOM 不会更新。
My case: I've got a download method that is not supposed to render anything.我的情况:我有一个不应该渲染任何东西的下载方法。

public function download()
{
   $this->skipRender();
}

public function render()
{
    if($this->shouldSkipRender) {
        return '';
    }
    
    return view("xxxxx");
}

As long as I'm not missing anything, this works for me.只要我没有遗漏任何东西,这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM