微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Livewire数据绑定问题

如何解决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";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。