【问题标题】:Dynamically push scripts using Laravel @push Blade Template Engine使用 Laravel @push Blade 模板引擎动态推送脚本
【发布时间】:2020-10-08 13:53:34
【问题描述】:

我有一个 Laravel 组件,它负责创建一个 select2 字段并绑定一个 livewire 属性。它工作得很好,但问题是我有一个页面需要一个动态表单,其中包含一个负责创建动态字段的添加字段按钮。这就是问题所在,在 Laravel 组件中使用 @push 指令包装的脚本代码没有被推送到 @stack,因为页面已经加载。

有人遇到过这种问题吗?请帮助我,我被困在这个问题上。我正在使用 Laravel 8.x 和 Livewire 2.x

输出得到: [1]:https://i.stack.imgur.com/bfZYP.png

选择两个刀片组件

<div wire:ignore>
    <select class="form-control select2" style="width: 100%" id="{{ $id }}" {{ $attributes }}>
        @if ($placeHolder)
            <option></option>
        @endif
        @foreach ($iteratable as $iteratableItem)
            <option value="{{ $value ? $iteratableItem->{$value} : $iteratableItem }}">
                {{ ucwords(implode(' ', explode('-', $label ? $iteratableItem->{$label} : $iteratableItem))) }}
            </option>
        @endforeach
    </select>

    @push('js')
    <script>
        $(document).ready(function() {
            let currentSelect = $('#{{ $id }}');
            let isMultiple = currentSelect.attr('multiple') ? true : false;
            let placeHolder = '{{ $placeHolder }}';
            let selectOptions = {
                placeholder: placeHolder,
                allowClear: isMultiple
            };
            
            currentSelect.select2(selectOptions);
            currentSelect.on('change', function() {
                let elementName = $(this).attr('wire:model');
                var data = $(this).val();
                @this.set(elementName, data);
            })
        });
    </script>
    @endpush
</div>

负责创建动态循环的foreach循环:

@foreach ($rater_type as $index => $raterField)
    <div class="form-group">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="rater_type-{{ $index }}">Rater Type</label>
                    <small class="required">Required</small>
                    <x-select-two-field name="rater_type[]" id="rater_type-{{ $index }}" place-holder="Select Rater Type #{{ $index + 1 }}" required :iteratable="$registeredRaterTypes" value="id" label="name" wire:model="rater_type.{{ $index }}" />
                    @error('rater_type.{{ $index }}') <small class="text-danger">{{ $message }}</small> @enderror
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label for="rater_count-{{ $index }}">Rater Count</label>
                    <small class="required">Required</small>
                    <input class="form-control" type="number" min="1" step="1" name="rater_count[]" id="rater_count-{{ $index }}" placeholder="Number of Raters for Rater Type #{{ $index + 1 }}" wire:model.debounce.600ms="rater_count.{{ $index }}" required />
                    @error('rater_count.{{ $index }}') <small class="text-danger">{{ $message }}</small> @enderror
                </div>
            </div>
        </div>
        <div class="d-flex justify-content-end">
            <div class="btn-group" role="group">
                @if ($loop->last)
                <button type="button" class="btn btn-success btn-sm" wire:click="addRaterField">
                    <i class="fas fa-plus mr-2"></i>
                    Add Rater
                </button>
                @endif
                @if (($loop->last && !$loop->first) || !$loop->last)
                <button type="button" class="btn btn-danger btn-sm" wire:click="removeRaterField({{ $index }})">
                    <i class="fas fa-close mr-2"></i>
                    Remove Rater
                </button>
                @endif
            </div>
        </div>
    </div>
    @endforeach

【问题讨论】:

  • 您可以添加您遇到的错误的屏幕截图吗?这会很有帮助。
  • @Gi1ber7 感谢您这么快回复。没有错误,只有一个 Livewire 警告说“Livewire 脚本已加载,请确保不要加载它们”,除了它只会显示 HTML 标记。
  • @BitzsAlex 你解决了吗?面临同样的问题!

标签: laravel laravel-livewire


【解决方案1】:

在你的 livevirefile 中添加发射函数,如

$this-&gt;emit('loadData');

并在刀片文件中调用此函数内的相关javascript函数

window.livewire.on('loadData', () => {

// your code

});

我认为它会起作用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2020-03-05
    • 2020-10-16
    • 2013-07-01
    • 2012-12-26
    • 2014-06-20
    • 2016-12-18
    相关资源
    最近更新 更多