【问题标题】:Livewire invalid arrow-function arguments on actionLivewire 动作无效的箭头函数参数
【发布时间】:2021-12-21 12:18:02
【问题描述】:

我正在使用 Laravel 购物车插件测试 Livewire。要将商品添加到购物车,您需要调用一个函数,传入 id、名称、数量、价格和可选的元数据数组:

<button wire:click.prevent="addToCart('{{ $id }}', '{{ $product_name}}', 1, {{ $price }}, ['size' => '{{ $size }}'])">Add to Cart</button>

呈现的 HTML 看起来很完美,但是当我单击按钮时,我得到:

Uncaught SyntaxError: invalid arrow-function arguments (parentheses around the arrow-function may help)

它似乎不喜欢数组赋值中的 =>。有人知道解决办法吗?

【问题讨论】:

    标签: javascript php laravel laravel-livewire


    【解决方案1】:

    在 javascript 中没有关联数组之类的东西,它是一个对象,因此您需要提供一个有效的对象(如果您仍然希望它具有相同的结构)。

    <button wire:click.prevent="addToCart('{{ $id }}', '{{ $product_name}}', 1, {{ $price }}, {{json_encode(['size' => $size ])}})">Add to Cart</button>
    

     @json(['size' => $size ]) //instead of {{json_encode(['size' => $size ])}}
    

    【讨论】:

    • 说得通,而且很管用,谢谢!
    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    相关资源
    最近更新 更多