【问题标题】:Multiple Blade X component instantiations in the same view同一视图中的多个 Blade X 组件实例化
【发布时间】:2020-07-02 18:00:08
【问题描述】:

你可以对一个 Blade X 组件进行多个实例化吗?

// app/view/components/foo.php

class Foo extends Component {
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    ...
// resources/views/components/foo.blade.php
<div>
    {{  $name }}
</div>
// resources/views/rootcomponent.blade.php
<x-foo name="John"></x-foo>
<x-foo name="Mary"></x-foo>

它说: 无法声明类 App\View\Components\Foo,因为该名称已在使用中

如果你使用它一次 - 或者在没有构造函数/类变量的情况下使用它,它就可以工作。 也许我会选择匿名方式,我只是好奇我做错了什么。

【问题讨论】:

    标签: laravel-blade laravel-7


    【解决方案1】:

    好像基类已经有了这个属性,所以不需要重新声明了。

    // app/view/components/foo.php
    
    class Foo extends Component {
    
        /* doesn't need to redeclare base class property */
        //public $name;
    
        public function __construct($name)
        {
            parent::__construct($name);
    
            /* doesn't need this line anymore. */
            // $this->name = $name;
    
            /* < Custom command line here.
                 ... >
    
                 But if there is no custom command line needed,
                 then this constructor is useless,
                 so no need to declare this.
              */
        }
    
        ...
    

    希望这可以帮助任何人。

    【讨论】:

    • 我不确定这个答案是否与Blade X components有关。
    • 我觉得laravel框架是基于php的,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多