[laravel]dropdown无法在@guest中工作

6tqwzwtp  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(99)

我在Laravel项目工作
下拉菜单在@guest内部不起作用,但在它外部起作用,尝试了许多解决方案,但一旦我将相同的代码放在@guest @endguest外部,它就像它应该的那样工作。

<ul class="navbar-nav ms-auto">
    <!-- Authentication Links -->
    @guest
        @if (Route::has('login'))
        <li class="nav-item ">
            <a class="nav-link button btn" href="{{ route('login') }}">{{ __('Login') }}</a>
        </li>
        @endif

        @if (Route::has('register'))
        <li class="nav-item">
            <a class="nav-link button btn" href="{{ route('register') }}">{{ __('Register') }}</a>
        </li>
        @endif
        @else
        <li class=" dropdown nav-item">
            <a id="navbarDropdown" class="nav-link dropdown-toggle"  role="button"
               data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
                {{ Auth::user()->name }}
            </a>

            <div class="dropdown-menu " aria-labelledby="navbarDropdown">
                <a class="dropdown-item" href="{{ route('logout') }}"
                   onclick="event.preventDefault();
                   document.getElementById('logout-form').submit();">
                    {{ __('Logout') }}
                </a>

                <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
                    @csrf
                </form>
            </div>
        </li>
    @endguest
</ul>
ugmeyewa

ugmeyewa1#

您没有正确使用@if @else语句,因为您以@endif结束if,并在结束if后启动@else
这个应该对你有用

<ul class="navbar-nav ms-auto">
    <!-- Authentication Links -->
    @guest
        @if (Route::has('login'))
        <li class="nav-item ">
            <a class="nav-link button btn" href="{{ route('login') }}">{{ __('Login') }}</a>
        </li>    
        @elseif (Route::has('register'))
        <li class="nav-item">
            <a class="nav-link button btn" href="{{ route('register') }}">{{ __('Register') }}</a>
        </li>
        @else
        <li class=" dropdown nav-item">
            <a id="navbarDropdown" class="nav-link dropdown-toggle"  role="button"
               data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
                {{ Auth::user()->name }}
            </a>

            <div class="dropdown-menu " aria-labelledby="navbarDropdown">
                <a class="dropdown-item" href="{{ route('logout') }}"
                   onclick="event.preventDefault();
                   document.getElementById('logout-form').submit();">
                    {{ __('Logout') }}
                </a>

                <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
                    @csrf
                </form>
            </div>
        </li>
        @endif
    @endguest
</ul>

相关问题