我正在制作一个组件结构,并添加了一个示例DataTable组件,我希望当我调用{{Component :: DataTable()}}时,datatable类具有脚本方法,应将其添加到应用程序中的js yield中刀片。
目录
app
|--Library
|--|--Components
|--|--|--DataTable
|--|--|--|--DataTable.php
|--|--|--|--DataTableBuilder.php
|--|--|Component.php
|--|--|IComponentBuilder.php
index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
{{
Component::DataTable()
}}
@yield('js')
</body>
</html>
DataTable.php
<?php
namespace App\Library\Components;
class DataTable {}
DataTableBuilder.php
<?php
namespace App\Library\Components;
class DataTableBuilder implements IComponentBuilder {
public function __construct()
{
$this->component = new DataTable();
}
public function script()
{
return '
console.log(\'test\');
';
}
public function toHtml()
{
return 'DataTable';
}
}
Component.php
<?php
namespace App\Library\Components;
class Component {
public static function DataTable()
{
$builder = new DataTableBuilder();
return $builder;
}
}
IComponentBuilder.php
<?php
namespace App\Library\Components;
use Illuminate\Contracts\Support\Htmlable;
interface IComponentBuilder extends Htmlable {
}
在这种情况下,如何将scripts()方法结果添加到js yield中。 PS:将来我可以添加多个组件。