【发布时间】:2023-04-05 06:56:01
【问题描述】:
我在 livewire 视图中有一个 select html 标记:
<select wire:model="categoryId" wire:change="$emit('attr_cat', $event.target.value)" name="category_id" class="form-control">
<option value="0">Select category</option>
@foreach($categories as $category)
<option value="{{$category->id}}">{{ $category->name }}</option>
@if(count($category->childs))
@include('admin.categories.subcategorylist',['childs' => $category->childs])
@endif
@endforeach
</select>
还有一个带有以下代码的 livewire 组件:
namespace App\Http\Livewire\Admin;
use App\Models\Attribute;
use App\Models\Category;
use App\Models\CategoryAttribute;
use Barryvdh\Debugbar\Facade as Debugbar;
use Livewire\Component;
class CategoryAttributes extends Component
{
public $attributeId;
public $categoryId;
public $attributeCategories;
public $categories;
protected $listeners = ['attr_cat' => 'addAttributeToCategory'];
public function mount()
{
$this->attributeCategories = Attribute::find($this->attributeId)->categories()->get();
$this->categories = Category::whereNull('category_id')->orderBy('name')->with('childs')->get();
$this->categoryId = 0;
}
public function render()
{
return view('livewire.admin.category-attributes');
// return view('livewire.admin.cat-attr-test');
}
public function addAttributeToCategory($value){
Debugbar::addMessage($value);
CategoryAttribute::create([
'category_id' => $this->categoryId,
'attribute_id' => $this->attributeId,
]);
}
public function updatedCategoryId(){
Debugbar::addMessage($this->attributeId);
}
}
并在我的刀片视图中使用这条线安装 livewire 组件:
@livewire('admin.category-attributes', ['attributeId' => $attribute->id], key('attr-'.$attribute->id))
但是,当我更改所选项目时,什么也没有发生,浏览器也没有向服务器发送 xhr 请求。
我也要这么说,我已经在 master Blade 文件中插入了 @livewireStyles 和 @livewireScripts 并且它们已正确加载。 感谢您的帮助。
【问题讨论】:
标签: xmlhttprequest laravel-livewire