簡體   English   中英

Livewire:如何重置 object 屬性?

[英]Livewire: How can I reset an object property?

我正在嘗試使用reset()方法做一些簡單的事情。

我的url model(對象)有一個title屬性,它綁定到我的刀片上的輸入字段:

<input
  type="text"
  wire:model.lazy="url.title"
  wire:keydown.escape="reset_fields"
  ...

在我的reset_fields function 我有:

public function reset_fields()
{
  $this->reset('url.title');
}

它顯示的錯誤是:

Livewire\Exceptions\PropertyNotFoundException
Property [$url.title] not found on component: [url-row]

我發現的解決方法如下:

<input
  type="text"
  wire:model.lazy="title"
  wire:keydown.escape="reset_fields"
  ...

在我的組件中:

...
public $title;
...

public function mount() {
  $this->title = $this->url->title;
}

public function reset_fields() {
  $this->mount();
}

所以是的,它有效,但我認為這不是“正確的方法”。 所以我的問題是,如何使用reset()方法,以及是否可以將它與對象一起使用

當直接綁定到 model 屬性如“url.title”時,你應該在規則中聲明模型屬性

public Url $url;
protected $rules = [
  'url.title' => 'required'
];
....
public function reset_fields()
{
   $this->url->title = null;
}

// blade
<input
  type="text"
  wire:model.lazy="url.title"
  wire:keydown.escape="reset_fields">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM