英文:
How do I know if an event was fired? Looks like it didn't
问题
I have a UserShow
component which shows the user's data (name, email, etc.) and also the actions he made in the site via relations (messages written, likes given, etc.). Now I want to allow the user to change some of his data (name, email, etc.), so I wrote another component called UserEdit
here is the form, validation, etc. It's working fine.
Since I'm using Livewire/Alpine I have some logic like
<div x-data="{ isEditing = @entangle('isEditing) }">
<section x-show="!isEditing" />
...All user data...
<button x-on:click="isEditing=true">
</section>
<section x-show="isEditing">
<livewire:user.user-edit :user="$user" />
</section>
</div>
So this works - it opens the component where the user will edit his data. In the component by the form's end, I have two buttons:
<div>
<button type="submit">update</x-gui.button>
<button type="button" wire:click="canceled">Cancel</button>
</div>
The canceled
function in the component is:
public function canceled() {
Log::info('Canceling Edit');
$this->emitUp('canceled');
}
Now, in the parent element I have the listeners and the function defined as:
protected $listeners = ['canceled' => 'restart'];
public function restart() {
Log::info('canceled received');
}
When I click the cancel method in the component, I see the first log "Canceling Edit," but I don't see the "canceled received" message.
Am I doing something wrong? How do I know if the "canceled" event is even being fired?
英文:
I have a UserShow
component which shows the user's data (name, email, etc.) and also the actions he made in the site via relations (messages written, likes given, etc.). Now I want to allow user to change some of his data (name, email, etc.), so I wrote another component called UserEdit
here is the form, validation, etc. It's working fine.
Since I'm using Livewire/Alpine I have some logic like
<div x-data="{ isEditing = @entangle('isEditing) }">
<section x-show="!isEditing" />
...All user data...
<button x-on:click="isEditing=true">
<section>
<section x-show="isEditing">
<livewire:user.user-edit :user="$user" />
</section>
</div>
So this works - it opens the component where the user will edit his data. In the component by the form's end, I have two buttons:
<div>
<button type="submit">update</x-gui.button>
<button type="button" wire:click="canceled">Cancel</button>
</div>
The canceled function in the component is:
public function canceled() {
Log::info('Canceling Edit');
$this->emitUp('canceled');
}
Now, in the parent element I have the listeners ans the function defined as:
protected $listeners = ['canceled' => 'restart'];
public function restart() {
Log::info('canceled received');
}
When I click the cancel method in the component, I see the first log "Canceling Edit", but I don't see the "canceled received" message
Am I doing something wrong? How do I know if the "canceled" event is even being fired?
- I have tried using
$emit('canceled')
from the button, but same thing without the log happens - I have also tried https://stackoverflow.com/questions/67289143/laravel-livewire-emit-event-not-fire
答案1
得分: 0
我发现了发生的事情,这是一个非常奇怪的行为,所以调试花了很长时间。
在这个非常具体的组件中的问题是,它在一个foreach循环中显示了所有的消息,像这样:
<div x-data="{ isEditing = @entangle('isEditing) }">
<section x-show="!isEditing" />
...所有用户数据... <=== 精确地在这里!!!
<button x-on:click="isEditing=true">
<section>
<section x-show="isEditing">
<livewire:user.user-edit :user="$user" />
</section>
</div>
所有用户数据都有一个 @foreach()
循环,用于渲染另一个组件:
@foreach($user->messages as $message)
<livewire:message-card :message="$message" />
@endforeach
问题是这个 Livewire 组件没有被标记,这导致了各种奇怪的问题发生,将行更改如下,解决了问题:
<livewire:message-card :message="$message" :key="$message->id" />
所以,这个警示故事是:无论组件是否只用于显示信息而不对其进行操作,始终在循环内为它们标记键。
英文:
I found what happened, and it was a very strange behaivour, so it tooks ages to debug.
The problem in this very specific component is that it was displaying all the messages inside a foreach, like:
<div x-data="{ isEditing = @entangle('isEditing) }">
<section x-show="!isEditing" />
...All user data... <=== PRECISELY HERE!!!
<button x-on:click="isEditing=true">
<section>
<section x-show="isEditing">
<livewire:user.user-edit :user="$user" />
</section>
</div>
The all user data had a @foreach()
that was rendering another component:
@foreach($user->messages as $message)
<livewire:message-card :message="$message" />
@endforeach
The problem was this livewire component wasn't being keyed, and this caused all kind of strange things to happen, chaging the line as below, solved the problem
<livewire:message-card :message="$message" :key="$message->id" />
So, the cautionary tale is: Always key your components inside a loop, even when they are just for displaying information, and not to be acted upon them
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论