首页 > 解决方案 > Livewire Uncaught (in promise) TypeError: Cannot read property 'split' of null

问题描述

我正在使用 Livewire 和 select2 来过滤某些表中的记录列表。这是多标准搜索。选择下拉菜单设置有“多个”选项。当我通过选择第一项进行过滤时,列表会更新。当我选择另一个项目时,列表会更新,但我得到了这个:

LoadingStates.js:192 Uncaught (in promise) TypeError: Cannot read property 'split' of null
at LoadingStates.js:192
at Array.forEach (<anonymous>)
at startLoading (LoadingStates.js:190)
at setLoading (LoadingStates.js:161)
at LoadingStates.js:50
at MessageBus.js:17
at Array.forEach (<anonymous>)
at MessageBus.value (MessageBus.js:16)
at Object.call (HookManager.js:38)
at Object.callHook (Store.js:120)

而且我不能选择另一个项目,这似乎会造成无限循环,因为我越来越多地得到它,我无法再做任何事情了。

选择是刀片组件。这是它的html:

<div>
@props(['datas', 'label'])
<div wire:ignore>
    <select class="form-control select2" {{ $attributes }}>
        @foreach($datas as $key => $item)
            <option value="{{ $item->id }}">{{ $item->$label }}</option>
        @endforeach
    </select>
</div>

在我的 livewire 组件中,我像这样使用它:

<div>
     <div class="form-group">
       <label for="myField1" class="font-weight-bold">Field 1 list</label>
       <x-utils.select2 class="form-control multiselect" name="myField1" multiple
                            wire:model="myField1" :datas="$field1datas" label="name"/>
     </div>
</div>
...

<table class="table">
    <tbody>
                        @if ($queryResults->count() > 0)
                        @foreach($queryResults as $key => $a)
                            <tr wire:key="{{ $loop->index }}">
                                <td>$a->content</td>
                            </tr>
                        @endforeach
                        @endif
    </tbody>
 </table>

我的 livewire 组件脚本是:

 function initSelect2() {
    $('.select2').select2({
        allowClear: true,
        placeholder: 'Veuillez faire un choix',
        language: "fr",
        width: "100%",
    }).on('change', function(e){
        console.log($(this).select2("val"));
        @this.set($( this).attr('name'), ( $(this).select2("val") ? $(this).select2("val") : $(this).val() ) );
    });
 }
 initSelect2();
 document.addEventListener("livewire:load", () => {
    Livewire.hook('message.processed', () => {
        initSelect2();
    });
});

似乎是错误

TypeError:无法读取 null 的属性“拆分”

是关于作为过滤结果重新调整的查询数据,但我不知道为什么。我在这里检查:https ://github.com/livewire/livewire/issues/2537但这对我没有帮助。如何解决这个问题?谢谢

标签: javascriptlaravellaravel-5laravel-livewirealpine.js

解决方案


你忘了关闭 div 标签。

<div>
    @props(['datas', 'label'])
    <div wire:ignore>
        <select class="form-control select2" {{ $attributes }}>
            @foreach($datas as $key => $item)
                <option value="{{ $item->id }}">{{ $item->$label }}</option>
            @endforeach
        </select>
    </div>
</div>

推荐阅读