本文介绍了Livewire数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to learn to use livewire. So I started with docs and screencasts. I was building a Laravel project using Jetstream with Livewire scaffolding.The problem seems to be that the controller does not pass the variables to the blade template.

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

I previously made a test project, using just Laravel 8, modifying the welcome.blade.php template and requiring Livewire by composer. And it worked just fine.

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

Steps to Reproduce: Create a Laravel 8.x jetstream project and use my code

这是我的代码:

在:App \ Http \ Livewire \ AddPost.php:

In: 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

In: resources/views/add-post.blade.php

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

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

        @livewireScripts
    </body>
</html>

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

In: resources\views\livewire\add-post.blade.php

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

GitHub存储库

推荐答案

尝试一下

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

OR

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

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

OR use this function, it is like a constructor().

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

这篇关于Livewire数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 18:24