【问题标题】:Laravel blade conditionals doesn't work in components when the condition is given by component method当条件由组件方法给出时,Laravel 刀片条件在组件中不起作用
【发布时间】: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()) 以调用该函数。

标签: php laravel-7


【解决方案1】:

你应该像这样使用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>

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2010-10-21
    • 2022-09-22
    • 1970-01-01
    相关资源
    最近更新 更多