简体   繁体   中英

Livewire invalid arrow-function arguments on action

I'm testing out Livewire with a Laravel shopping cart plugin. To add items to the cart you call a function, passing in id, name, quantity, price, and an optional array of metadata:

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

The rendered HTML looks perfect, but when I click the button I get:

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

It doesn't seem to like the => in the array assignment. Does anyone know a fix?

there is no such thing as an associative array in javascript, it's an object and as such you need to provide a valid object (if you still want it with the same structure).

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM