我有以下 HTML:
<div class="relative lg:inline-flex bg-gray-100 rounded-xl">
<x-dropdown>
<x-slot:trigger>
<button>.......</button>
</x-slot:trigger>
<x-dropdown-item href='/' :active="request()->is('/')">All</x-dropdown-item>
@foreach($categories as $category)
<x-dropdown-item href="category/{{ $category->slug }}"
:active="request()->is('/category/'.$category->slug)">
{{ ucwords($category->name) }}
</x-dropdown-item>
@endforeach
</x-dropdown>
</div>
但是我的下拉项组件中未分配 active 属性:
@props(['active' => false])
@php
$classes = 'block text-left px-3 text-sm leading-5 hover:bg-blue-500 focus:bg-blue-500 hover:text-white focus:text-white';
if ($active) {
dd($active);
$classes .=' bg-blue-500 text-white';
}
@endphp
<a {{ $attributes(['class' => $classes]) }}>{{ $slot }}</a>
我希望当 active 属性设置为 true 时我的 dd 会触发,但它永远不会触发
我尝试制作一个 DropdownItem 类:
<?php
namespace App\View\Components;
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class DropdownItem extends Component
{
/**
* Create a new component instance.
*/
public function __construct(...$args)
{
dd($args);
// $this->active = true;
}
/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.dropdown-item', [
// 'active' => $this->active
]);
}
}
但是 $args 返回 [] 所以也没有属性,我不知道为什么,我检查了 laravel 10 的官方文档,但一切似乎都很好。如果我在课堂上手动设置为活动状态,一切都会正常
<x-dropdown-item href="category/{{ $category->slug }}"
而不是
<x-dropdown-item href="/category/{{ $category->slug }}"
无需上课即可解决问题。如果我添加一个类,类构造函数仍然没有获得活动属性。还在想办法