Livewire数据绑定问题

问题描述

我正在尝试学习使用livewire。所以我从文档和截屏开始。我当时在使用带有Livewire脚手架的Jetstream构建Laravel项目。 问题似乎是控制器没有将变量传递给刀片模板。

我以前仅使用Laravel 8进行了一个测试项目,修改welcome.blade.PHP模板,并要求作曲家使用Livewire。而且效果很好。

重现步骤:创建一个Laravel 8.x jetstream项目并使用我的代码

这是我的代码

在:App \ Http \ Livewire \ AddPost.PHP

<?PHP

namespace App\Http\Livewire;

use Livewire\Component;

class AddPost extends Component
{
    public $title = "Blank";
    public $content = "Such empty here";
    public function render()
    {
        return view('livewire.add-post');
    }
}

在:资源/视图/add-post.blade.PHP

<html>
    <head>
        @livewireStyles
    </head>

    <body>
        @livewire('add-post')

        @livewireScripts
    </body>
</html>

在:资源\视图\ livewire \ add-post.blade.PHP

<div>
    Title: {{ $title }}
    <br>
    Content: {{ $content }}
</div>

GitHub repo

解决方法

尝试一下

public function render()
{
    return view('livewire.add-post',[
        'title' => $this->title,'content' => $this->content
    });
}

OR

@livewire('add-post',['title' => 'lorem ipsum','' => 'content'])

或使用此函数,就像构造函数()。

public function mount() {
    $this->title = 'lorem ipsum';
    $this->content = 'content';
}
,

在 app.blade.php 中试试这个:{{$slot}}

 <html>
     <head>
         @livewireStyles
     </head>
 
     <body>
         {{ $slot }}                   <<<<<<<--------------
 
         @livewireScripts
     </body>
 </html>
,

你可以使用这个

public $title;
public $content;

public function mount(){
$this->title= "Blank";
$this->content= "Such empty here";
}