如何解决LiveWire 方法不适用于简单组件
我创建了一个新的简单组件用于 liveWire
并且我正在尝试从单个路由中使用此组件,在下面的简单实现中 increment
和 decrement
对我不起作用
路线:
Route::get('/login',LoginComponent::class);
LoginComponent::class
:
class LoginComponent extends Component
{
public $count = 10;
public function render()
{
return view('livewire.auth.login')->layout('livewire.auth.app');
}
public function increment()
{
$this->count++;
}
public function decrement()
{
$this->count--;
}
}
app.blade.php
:
<!DOCTYPE html>
<html lang="en">
<head>
...
@livewireStyles
</head>
<body>
<div class="page-content">
<div class="content-wrapper">
@yield('content')
</div>
</div>
@livewireScripts
</body>
</html>
然后login.blade.php
:
<div>
@section('content')
<div class="page-content">
<div class="content-wrapper">
<div class="content d-flex justify-content-center align-items-center">
<div style="text-align: center">
<button wire:click="increment"> + </button>
<button wire:click="decrement"> - </button>
<h1>{{ $count }}</h1>
</div>
</div>
</div>
</div>
@endsection
</div>
解决方法
我建议使用适当的渲染方法。
return view('livewire.auth.login')
->extends('livewire.auth.app')
->section('content');
并从 login.blade.php 中删除 @section('content')
检查 Rendering Components 上的文档
,我已经尝试了 Livewire 文档中描述的较旧的 @yield/@section Laravel 样式,并且没有问题。
你的 app.blade.php 看起来没问题。
但是您的 LoginController 渲染方法必须使用 extends():
public function render()
{
return view('livewire.auth.login')->extends('livewire.auth.app');
}
并且您应该从 login.blade.php 组件中删除 @section。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。