Laravel 10 – Livewire wire:clicks won't work

huangapple go评论72阅读模式
英文:

Laravel 10 - Livewire wire:clicks won't work

问题

I'm using Livewire 2.12 and Laravel 10.10.

我正在使用Livewire 2.12和Laravel 10.10。

I have a problem in Livewire, when I'm clicking on the sign in button, the "login" function in the Login.php component is not executed.

在Livewire中,当我点击登录按钮时,Login.php组件中的“login”函数未被执行。

Could the reason be that I'm using the "@yield('content')" sections and not the "{{ $slot }} ?
If so, how to fix the issue? because if I used the "{{ $slot }}" instead of the "@yield('content')", I get an error that the $slot variable is undefined.

可能的原因是我在使用"@yield('content')"部分而不是"{{ $slot }}"。如果是这样,该如何解决这个问题?因为如果我使用"{{ $slot }}"而不是"@yield('content')",我会收到$slot变量未定义的错误。

I have this base layout:

我有这个基本布局:

base.blade.php :

base.blade.php:




// all imported styles...
@livewireStyles


@yield('content')

// all imported scripts...
@livewireScripts


and here is my login view login.blade.php:

这是我的登录视图login.blade.php:

@extends('layouts.base')

@section('content')

 <div class="mb-3">
     <label for="email" class="form-label">Email</label>
     <input
          wire:model="email"
          type="text"
          class="form-control"
          id="email"
          name="email"
          placeholder="Enter your email"
          autofocus
          required
     />
 </div>
 <div class="mb-3">
     <button wire:click="login">Sign in</button>
 </div>                 

@endsection

and here is my livewire component Login.php:

这是我的Livewire组件Login.php:

user())
{
redirect('/dashboard');
}
}

public function login()
{
dd('efefeff');
}

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

英文:

I'm using Livewire 2.12 and Laravel 10.10.

I have a problem in Livewire, when I'm clicking on the sign in button, the "login" function in the Login.php component is not executed.

Could the reason be that I'm using the "@yield('content')" sections and not the "{{ $slot }} ?
If so, how to fix the issue? because if I used the "{{ $slot }}" instead of the "@yield('content')", I get an error that the $slot variable is undefined.

I have this base layout:

base.blade.php :

<!DOCTYPE html>

<html>
<head>
// all imported styles...
@livewireStyles
</head>

<body>
@yield('content')

// all imported scripts...
@livewireScripts
</body>

</html>

and here is my login view login.blade.php:

@extends('layouts.base')

@section('content')

<div class="container">

<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input
wire:model="email"
type="text"
class="form-control"
id="email"
name="email"
placeholder="Enter your email"
autofocus
required
/>
</div>
<div class="mb-3">
<button wire:click="login">Sign in</button>
</div>

</div>

@endsection

and here is my livewire component Login.php:

<?php

namespace App\Http\Livewire\Auth;

use Livewire\Component;

class Login extends Component
{
public $email = '';

public function mount()
{
if(auth()->user())
{
redirect('/dashboard');
}
}

public function login()
{
dd('efefeff');
}

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

# 答案1
**得分**: 0

你需要在`login.blade.php`中包含组件,而不是仅包含按钮本身。

假设组件位于 `Livewire\Component\Login` 中。

将原先的:

```html

```

改为:

```html ```

然后将按钮移动到 Livewire 的登录组件中。

英文:

You need to include the component in the login.blade.php not the button itself.

Assuming the component is in `Livewire\Component\Login`

Instead of:

<button wire:click="login">Sign in</button>
Add the livewire component itself

<livewire:components.login />

And move the button to the livewire Login componenet.

# 答案2
**得分**: 0

我已修复它。

我已将 base.blade.php 文件中的 @yield('content') 更改为 {{ $slot }},然后在 login.blade.php 文件中进行了以下更改:


英文:

I fixed it.

I have changed the @yield('content') in the base.blade.php file into {{ $slot }},
then made these changes in the login.blade.php file :

<x-layouts.base>

<div class="container">

<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input
wire:model="email"
type="text"
class="form-control"
id="email"
name="email"
placeholder="Enter your email"
autofocus
required
/>
</div>
<div class="mb-3">
<button wire:click="login">Sign in</button>
</div>

</div>

</x-layouts.base>

huangapple
  • 本文由 发表于 2023年7月10日 19:17:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定