剛開始使用 Laravel Livewire,我在將引數傳遞給 livewire 組件時遇到問題。
主頁.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
@livewireStyles
</head>
<body class="antialiased p-10">
<livewire:forms.input type="text" name="Test" />
@livewireScripts
</body>
</html>
零件
<div class="FormsInput">
@isset($label)
<label for="{{ $name }}">{{ $label }}</label>
@endisset
<div hljs-string">">
<input wire:model="{{ $name }}" type="{{ $type }}" name="{{ $name }}" id="{{ $name }}" placeholder="{{ $placeholder }}">
</div>
@isset($helper)
<div hljs-string">">
{{ $label }}
</div>
@endisset
<div hljs-string">">
{{ $error }}
</div>
</div>
Http/Livewire/表單/輸入
<?php
namespace App\Http\Livewire\Forms;
use Livewire\Component;
class Input extends Component
{
public function render()
{
return view('livewire.forms.input');
}
}
這是我得到的錯誤:
ErrorException
Undefined variable $name

uj5u.com熱心網友回復:
要修復此錯誤,您必須做兩件事:
- 將 name 屬性添加到您的組件,因為 Livewire 組件將資料存盤和跟蹤為組件類上的公共屬性。
class Input extends Component
{
public string $name;
...
}
- 在電線上使用屬性名稱:沒有“$”的模型
<input wire:model="name" ... />
您可以在此鏈接上找到 Livewire 檔案上的資料系結。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/439799.html
上一篇:在Laravel中確定url引數
