我有以下超文本标记语言:

<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>

但在我的DropDown-Item组件中没有分配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>

我希望我的dd在active属性设置为true时触发,但它从未触发

我try 创建一个Dropdown Item类:

<?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=".." :active="request()->is('category/'.$category->slug)"> 
{{ .. }}</x-dropdown-item>

而不是

<x-dropdown-item href=".." :active="request()->is('/category/'.$category->slug)"> 
{{ .. }}</x-dropdown-item>

确保路径以‘/’开头.

这在没有Dropdown Item类的情况下解决了这个问题.

如果我想使用DropdownItem类,我必须:

remove @props(['active' => false]) (not required)
php artisan optimize:clear

Laravel相关问答推荐

如何更改LIVE上的Filament占位符内容?

Laravel本地化错误:"setlocale():参数#1($category)的类型必须是int,给定的是Illuminate\Http\Request"

如何在 Laravel 中将 Select 选项表单插入数据库

如何在数据来自Rest API的flutter中创建下拉类别名称列表?

如何在 laravel 5.3 中验证没有 auth:api 中间件的用户?

集成测试模拟外观与注入模拟

在 apache docker 容器中运行虚拟主机

Laravel 路由将变量传递给控制器

为什么人们将 .env 放入 gitignore?

laravel 5中的配置缓存导致找不到视图

Laravel 将参数从路由传递到过滤器

如何对模型执行删除操作?

Laravel 同步错误

如何使用中间件将标头添加到响应中?

Laravel 说 Auth guard [] 没有定义

测试指向外部站点的重定向链接

Laravel Eloquent ORM 复制

Laravel Mix 生成字体到另一个目录

在 Laravel 5 控制器中找不到类App\Http\Controllers\DB

如何在 Laravel 中正确安装软件包?