【发布时间】:2020-12-12 00:31:46
【问题描述】:
我创建了一个 Laravel 刀片组件。在其中我有一个名为“某事”的方法,它需要评估一些逻辑。
然后在组件视图中,我根据条件显示一些信息。
如果条件返回真,则显示信息。但是当它为假时,什么都不会发生。
app/View/Components/ExampleComponent.php
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class ExampleComponent extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}
public function something(){
if (2 + 2 === 4) return true; // some logic
else return false;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return view('components.example-component');
}
}
resources/views/components/example-component.blade.php
<div>
@if(something)
<h1>
2 + 2 = 4
</h1>
@else
<h1>
That is witchery <!-- This doesn't show if I evaluate 2 + 3 === 4 -->
</h1>
@endif
</div>
【问题讨论】:
-
嗨!您应该将
@if(something)更改为@if(something())以调用该函数。