LARAVEL 7刀片组件公共方法不起作用

人气:501 发布:2022-10-16 标签: php laravel components laravel-blade laravel-7

问题描述

我的Laravel项目在使用blade component时遇到问题。

当我定义新组件并希望使用";组件公共方法&向";查看组件刀片文件";传递数据时,我收到一个错误,&q;未定义变量&q;

该组件的制造者

php artisan make:component testc 

我试了

cache:clear

view:clear

composer dump之前

,编码如下:

组件编码

<?php

namespace AppViewComponents;

use IlluminateViewComponent;

class testc extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return IlluminateViewView|string
     */
    public function render()
    {
        return view('components.testc');
    }

    public function test()
    {
        return 'test';
    }
}

查看组件刀片文件

<div>
    {{ $test }}
</div>

和我的错误

ps:laravel版本为:7.18.0

推荐答案

在刀片文件中,您应该这样调用组件的方法:
<div>
   {{ $test() }}
</div>
注意方括号。

613