Blade组件在使用Alpinejs时未显示。

huangapple go评论65阅读模式
英文:

Blade component not showing using Alpinejs

问题

I tried to create a simple Laravel app with Alpine.js installed locally via Yarn. I followed a simple example from YT about creating Laravel blade components and it's working fine using the CDN version. But, not working on Alpine.js installed locally, and got the error below.

resources/views/components/input/group.blade.php

@props(['label', 'error' => null])

<div x-data x-id="[‘input’]" {{ $attributes }}>
    <label x-bind:for=" $id('input') " {{ $attributes->class('block mb-2 text-sm font-medium text-gray-900') }}>
        {{ $label }}
    </label>

    {{ $slot }}
</div>

@if ($error)
    <div class="text-sm text-red-600 !mt-1">{{ $error }}</div>
@endif

resources/views/components/input/select.blade.php

@aware(['error'])
@props(['error' => null])

<div>
    <select x-bind:id=" $id('input') "
        {{ $attributes->class([
            'bg-gray-50 border text-gray-900 text-sm rounded focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5',
            'border-gray-300' => !$error,
            'border-red-500' => $error,
        ]) }}>
        {{ $slot }}
    </select>
</div>

index.blade.php

<!DOCTYPE html>
<html lang="en" class="min-w-[300px]">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>test</title>
    <script src="{{ mix('js/alpine.js') }}"></script>
    <link rel="stylesheet" href="{{ mix('css/tailwind.css') }}">
</head>

<body class="flex flex-col items-center">
    @verbatim
        <div class="max-w-[768px] min-h-screen bg-cover bg-center bg-black">
            <div class="p-8">
                <img src="test.jpg">

                <div x-data="alpineMixin" x-cloak class="space-y-3">
                    <x-input.group label="Your Age" class="text-white" :error="$errors->first('age')">
                        <x-input.select x-model="profile.age" name="age">
                            <template x-for="n in 53" :key="n">
                                <option :value="n + 17" x-text="n === 53 ? '70+' : n + 17" :selected="profile.age == n + 17"></option>
                            </template>
                        </x-input.select>
                    </x-input.group>
                </div>
            </div>
        </div>
    @endverbatim

    <script src="/js/manifest.js"></script>
</body>

</html>

Please note that I want to use Alpine.js installed via Yarn.

英文:

I tried to create a simple Laravel app with Alpinejs installed locally via Yarn. I followed a simple example from YT about creating Laravel blade components and it's working fine using the CDN version. But, not working on Alpinejs installed locally, and got the error below. Blade组件在使用Alpinejs时未显示。

resources/views/components/input/group.blade.php

@props([&#39;label&#39;, &#39;error&#39; =&gt; null])
&lt;div x-data x-id=&quot;[&#39;input&#39;]&quot; {{ $attributes }}&gt;
&lt;label x-bind:for=&quot;$id(&#39;input&#39;)&quot; {{ $attributes-&gt;class(&#39;block mb-2 text-sm font-medium text-gray-900&#39;) }}&gt;
{{ $label }}
&lt;/label&gt;
{{ $slot }}
&lt;/div&gt;
@if ($error)
&lt;div class=&quot;text-sm text-red-600 !mt-1&quot;&gt;{{ $error }}&lt;/div&gt;
@endif

resources/views/components/input/select.blade.php

@aware([&#39;error&#39;])
@props([&#39;error&#39; =&gt; null])
&lt;div&gt;
&lt;select x-bind:id=&quot;$id(&#39;input&#39;)&quot;
{{ $attributes-&gt;class([
&#39;bg-gray-50 border text-gray-900 text-sm rounded focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5&#39;,
&#39;border-gray-300&#39; =&gt; !$error,
&#39;border-red-500&#39; =&gt; $error,
]) }}&gt;
{{ $slot }}
&lt;/select&gt;
&lt;/div&gt;

index.blade.php

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; class=&quot;min-w-[300px]&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;title&gt;test&lt;/title&gt;
&lt;script src=&quot;{{ mix(&#39;js/alpine.js&#39;) }}&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;{{ mix(&#39;css/tailwind.css&#39;) }}&quot;&gt;
&lt;/head&gt;
&lt;body class=&quot;flex flex-col items-center&quot;&gt;
@verbatim
&lt;div class=&quot;max-w-[768px] min-h-screen bg-cover bg-center bg-black&quot;&gt;
&lt;div class=&quot;p-8&quot;&gt;
&lt;img src=&quot;test.jpg&quot;&gt;
&lt;div x-data=&quot;alpineMixin&quot; x-cloak class=&quot;space-y-3&quot;&gt;
&lt;x-input.group label=&quot;Your Age&quot; class=&quot;text-white&quot; :error=&quot;$errors-&gt;first(&#39;age&#39;)&quot;&gt;
&lt;x-input.select x-model=&quot;profile.age&quot; name=&quot;age&quot;&gt;
&lt;template x-for=&quot;n in 53&quot; :key=&quot;n&quot;&gt;
&lt;option :value=&quot;n + 17&quot; x-text=&quot;n === 53 ? &#39;70+&#39; : n + 17&quot;
:selected=&quot;profile.age == n + 17&quot;&gt;&lt;/option&gt;
&lt;/template&gt;
&lt;/x-input.select&gt;
&lt;/x-input.group&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
@endverbatim
&lt;script src=&quot;/js/manifest.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

> Please note that I want to use alpinejs installed via yarn.

答案1

得分: 0

Sure, here is the translated content:

<script src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/2.3.0/alpine-ie11.min.js" integrity="sha512-Atu8sttM7mNNMon28+GHxLdz4Xo2APm1WVHwiLW9gW4bmHpHc/E2IbXrj98SmefTmbqbUTOztKl5PDPiu0LD/A==" crossorigin="anonymous"></script>

You should try using the above CDN link in your index blade.

英文:
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/alpinejs/2.3.0/alpine-ie11.min.js&quot; integrity=&quot;sha512-Atu8sttM7mNNMon28+GHxLdz4Xo2APm1WVHwiLW9gW4bmHpHc/E2IbXrj98SmefTmbqbUTOztKl5PDPiu0LD/A==&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

you should try this above link CDN in your index blade

答案2

得分: 0

Removing the @verbatim solves my issue. I think it's interfering with the rendering of Alpinejs components preventing the dynamic behavior from working correctly.

英文:

Hmmm, removing the @verbatim solves my issue. I think it's interfering with the rendering of Alpinejs components preventing the dynamic behavior from working correctly.

huangapple
  • 本文由 发表于 2023年5月10日 15:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定