如何解决当条件通过组件方法给出时,Laravel刀片条件在组件中不起作用
我已经创建了Laravel刀片组件。在它的内部,我有一个称为“某物”的方法,该方法需要评估一些逻辑。
然后在组件视图中,根据条件显示一些信息。
如果条件返回true,则显示信息。但是当它为假时,什么也不会发生。
应用程序/视图/组件/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');
}
}
资源/视图/组件/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>
解决方法
您应像这样使用something
:
<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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。