1

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

我之前做了一个测试项目,只使用 Laravel 8,修改了welcome.blade.php模板并需要 Composer 的 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 仓库

4

3 回答 3

2

尝试这个

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

或者

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

或者使用这个函数,它就像一个构造函数()。

public function mount() {
    $this->title = 'lorem ipsum';
    $this->content = 'content';
}
于 2020-11-30T18:36:34.773 回答
0

你可以用这个

public $title;
public $content;

public function mount(){
$this->title= "Blank";
$this->content= "Such empty here";
}
于 2021-03-27T05:06:10.607 回答
0

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

 <html>
     <head>
         @livewireStyles
     </head>
 
     <body>
         {{ $slot }}                   <<<<<<<--------------
 
         @livewireScripts
     </body>
 </html>
于 2021-01-12T06:23:48.563 回答