W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
您可以使用 HTML 屬性傳遞數(shù)據(jù)到 Blade 組件中。普通的值可以通過簡單的 HTML 屬性來傳遞給組件。PHP 表達式和變量應(yīng)該通過以 :
字符作為前綴的變量來進行傳遞:
<x-alert type="error" :message="$message"/>
您應(yīng)該在類的構(gòu)造器中定義組件的必要數(shù)據(jù)。在組件的視圖中,組件的所有 public 類型的屬性都是可用的。不必通過組件類的 render
方法傳遞:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Alert extends Component
{
/**
* alert 類型。
*
* @var string
*/
public $type;
/**
* alert 消息。
*
* @var string
*/
public $message;
/**
* 創(chuàng)建一個組件實例。
*
* @param string $type
* @param string $message
* @return void
*/
public function __construct($type, $message)
{
$this->type = $type;
$this->message = $message;
}
/**
* 獲取組件的視圖 / 內(nèi)容
*
* @return \Illuminate\View\View|\Closure|string
*/
public function render()
{
return view('components.alert');
}
}
渲染組件時,您可以回顯變量名來顯示組件的 public 變量的內(nèi)容:
<div class="alert alert-{{ $type }}">
{{ $message }}
</div>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: