2

我尝试从 livewire 组件传递一个变量:

class Index extends Component
{
    public function render()
    {
        return view('livewire.index')->with('type', config('constants.NONAUTH'));
    }
}

并从 layouts.app 访问它:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

{{dd($type)}}

@include('includes.head')

...

我收到一个$type未定义的错误,这样做的正确方法是什么?

4

1 回答 1

0

声明一个公共变量 $type 并在你的 mount() 函数中设置这个变量,如下所示:

class Index extends Component
{
    public $type = null;
    
    public function render()
    {
        return view('livewire.index');
    }

    public function mount() 
    {
         $this->type = config('constants.NONAUTH');
    }

}

编辑:

我明白了,我第一次误会你,。我再次阅读并了解到您想要的东西不会起作用,因为您正在尝试访问在您的应用程序布局之后声明的变量。Livewire 在布局加载后立即渲染其组件。我建议您使用 livewire 制作整个应用程序布局。

是的,可以通过多种方式将 livewire 中的某些内容声明到刀片中。这是一个:

您可以触发一个事件,该事件可以被刀片内的全局 JS 监听。然后您可以使用 jquery / vanilla js 在刀片组件中设置该值。

于 2020-08-16T12:15:26.647 回答