首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel Livewire wire:单击not wire

Laravel Livewire wire:单击not wire
EN

Stack Overflow用户
提问于 2021-06-12 19:35:39
回答 1查看 639关注 0票数 0

我正在使用bumbummen99 shopping cart开发一个基于Laravel和Livewire的电子商务平台。我的问题是,我只能向购物车添加项目,但由于某些原因,我无法更新(增加或减少)数量,从购物车中删除项目。I have no error in the console,则在单击这两个按钮时不会发生任何网络活动。I have checked that all the HTML is enclosed in one <div>.我已经查看了其他几个页面(hereherehere),但没有成功。我还试着更改不工作的导线:单击以遵循导线的格式:单击正在工作的addToCart,这也没有帮助。任何帮助都是非常感谢的。

这是我的产品列表

代码语言:javascript
复制
@extends('layouts.app')

@section('template_linked_css')
<link href="{{asset('front/css/cart.css')}}" rel="stylesheet">

@endsection

@section('content')
<main class="bg_gray">
    @livewire('cart-listing')
</main>
@endsection

下面是我的CartListing组件:

代码语言:javascript
复制
use App\Models\Service;
use Gloudemans\Shoppingcart\Facades\Cart;
use Livewire\Component;

class CartListing extends Component
{
public $products;
public array $quantity = [];

public function mount()
{
    $this->products = Cart::content();
    foreach ($this->products as $product) {
        $this->quantity[$product->id] = 1;
    }
}

public function addToCart($product_id)
{
    $product = Product::findOrFail($product_id);
    Cart::add(
        $product->id,
        $product->name,
        $this->quantity[$product_id],
        $product->price
    );

    $this->emit('cart_updated');
}

public function increase($rowId){
    Cart::update($rowId, 1);
    $this->emit('cart_updated');
}

public function decrease($rowId){
    Cart::update($rowId, -1);
    $this->emit('cart_updated');
}

public function deleteItem($rowId){
    Cart::remove($rowId);
    $this->emit('cart_updated');
}

public function render()
{
    $cart = Cart::content();
    $cart_count = Cart::content()->count();
    $cart_total = Cart::subtotal();

    return view('livewire.cart-listing', compact('cart', 'cart_count', 'cart_total'));
}



}

这是我的livewire购物车列表视图:

代码语言:javascript
复制
<div>
    <div class="container margin_30">
        <div class="page_header">
            <div class="breadcrumbs">
                <ul>
                    <li><a href="#">Home</a></li>
                    <li>Cart</li>
                </ul>
            </div>
            <h1>Cart</h1>
        </div>
        <!-- /page_header -->
        <table class="table table-striped cart-list">
            <thead>
            <tr>
                <th>
                    Product
                </th>
                <th>
                    Price
                </th>
                <th>
                    Quantity
                </th>
                <th>
                    Subtotal
                </th>
                <th>

                </th>
            </tr>
            </thead>
            <tbody>
            @forelse ($products as $product)
            <tr>
                <td>
                    <div class="thumb_cart">
                        <img src="{{asset('front/img/products/product_placeholder_square_small.jpg')}}" data-src="{{asset('front/img/products/shoes/1.jpg')}}" class="lazy" alt="Image">
                    </div>
                    <span class="item_cart">{{$product->name}}</span>
                </td>
                <td>
                    <strong>£ {{$product->price}}</strong>
                </td>
                <td>
                    <div class="numbers-row">
                        <input type="text" value="{{$product->qty}}" id="quantity_1" class="qty2" name="quantity_1">
                        <div wire:click="increase({{$product->rowId}})" class="inc button_inc">+</div>
                        <div wire:click="decrease({{$product->rowId}})" class="dec button_inc">-</div>
                    </div>
                </td>
                <td>
                    <strong>£ {{number_format($product->price * $product->qty, 2,'.',',')}}</strong>
                </td>
                <td class="options">
                    <a wire:click="deleteItem({{$product->rowId}})"><i class="ti-trash"></i></a>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="3">
                    <span class="item_cart">Your cart is empty</span>
                </td>
            </tr>
            @endforelse
            </tbody>
        </table>
    </div>
    <!-- /container -->

    <div class="box_cart">
        <div class="container">
            <div class="row justify-content-end">
                <div class="col-xl-4 col-lg-4 col-md-6">
                    <ul>
                        <li>
                            <span>Subtotal</span> $ {{$cart_total}}
                        </li>
                        <li>
                            <span>Total</span> $ {{$cart_total}}
                        </li>
                    </ul>
                    <a href="cart-2.html" class="btn_1 full-width cart">Proceed to Checkout</a>
                </div>
            </div>
        </div>
    </div>
    
</div>

我的layouts.app也有livewire链接:

代码语言:javascript
复制
<head>
    @livewireStyles
</head>
<body >
@livewireScripts
</body>
EN

回答 1

Stack Overflow用户

发布于 2021-06-14 04:47:30

如果要将字符串传递给wire:click方法,则应使用单引号将其封装,如下所示

代码语言:javascript
复制
wire:click="increase('{{$product->rowId}}')"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67948474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档