首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多个Livewire“更新”生命周期挂钩组合成一个函数

将多个Livewire“更新”生命周期挂钩组合成一个函数
EN

Stack Overflow用户
提问于 2022-06-13 01:45:08
回答 1查看 367关注 0票数 0

当用户更改宽度、高度和/或长度的值时,我需要更新页面上的价格。下面的代码可以工作,但它看起来可能更干净,而不是有三个单独的updated生命周期挂钩与重复的代码。在挂载视图时,我设置宽度、高度和长度的默认值,并计算价格。

show.php

代码语言:javascript
复制
...
class Show extends Component
{
    public $product;
    public $sku;
    public $name;
    public $description;
    public $width;
    public $height;
    public $length;
    public $price;

    public function mount(Product $product){
        $this->product = $product;
        $this->sku = $product->sku;
        $this->name = $product->name;
        $this->description = $product->description;

        $this->width = 4;
        $this->height = 4;
        $this->length = 10;
        $this->price = (((($this->width + $this->height + $this->height)*12)/144)*25)*$this->length;
    }

    public function updatedWidth() {
        $width = $this->width;
        $height = $this->height;
        $length = $this->length;
        $this->price = (((($width + $height + $height)*12)/144)*25)*$length;
    }

    public function updatedHeight() {
        $width = $this->width;
        $height = $this->height;
        $length = $this->length;
        $this->price = (((($width + $height + $height)*12)/144)*25)*$length;
    }

    public function updatedLength() {
        $width = $this->width;
        $height = $this->height;
        $length = $this->length;
        $this->price = (((($width + $height + $height)*12)/144)*25)*$length;
    }

    public function render()
    {
        return view('livewire.shop.show')->layout('layouts.frontend');
    }
}

show.blade.php

代码语言:javascript
复制
<!-- ... -->
<div class="mt-10 px-4 sm:px-0 sm:mt-16 lg:mt-0">
    <h1 class="text-3xl font-extrabold tracking-tight text-gray-900">{{$product->name}}</h1>

    <div class="mt-3">
        <h2 class="sr-only">Product information</h2>
        <p wire:model="price" class="text-3xl text-gray-900">${{number_format($price, 2, '.')}}</p>
    </div>

    <div class="mt-6">
        <h3 class="sr-only">Description</h3>

        <div class="text-base text-gray-700 space-y-6">
            <p>
                {{$product->description}}
            </p>
        </div>
    </div>

    <form wire:submit.prevent="createOrderItem" method="POST" class="mt-6">
        <div>
            <h3 class="text-sm text-gray-600">Size</h3>

            <div class="grid grid-cols-4 gap-4">
                <div class="mt-4 sm:mt-0 sm:pr-9">
                    <label for="width" class="block text-sm font-medium text-gray-700">Outer Width:</label>
                    <select wire:model="width" id="width" name="width" class="mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
                        <option value="3">3"</option>
                        <option value="4">4"</option>
                        <option value="5">5"</option>
                    </select>
                </div>

                <div class="mt-4 sm:mt-0 sm:pr-9">
                    <label for="height" class="block text-sm font-medium text-gray-700">Outer Height:</label>
                    <select wire:model="height" id="height" name="height" class="mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
                        <option value="3">3"</option>
                        <option value="4">4"</option>
                        <option value="5">5"</option>
                    </select>
                </div>

                <div class="mt-4 sm:mt-0 sm:pr-9">
                    <label for="length" class="block text-sm font-medium text-gray-700">Length:</label>
                    <select wire:model="length" id="length" name="length" class="mt-1 block w-full pl-3 pr-10 py-2 text-base border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md">
                        <option value="10">10'</option>
                        <option value="11">11'</option>
                        <option value="12">12'</option>
                    </select>
                </div>
            </div>
        </div>
<!-- ... -->
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-13 06:24:51

由于您正在多次执行相同的计算,所以将其提取到自己的方法中是有意义的。然后,您可以使用“全局”updated()生命周期-钩子,并有条件地设置价格的基础上,字段被更新。

代码语言:javascript
复制
class Show extends Component
{
    public $product;
    public $sku;
    public $name;
    public $description;
    public $width;
    public $height;
    public $length;
    public $price;

    public function mount(Product $product) 
    {
        $this->product = $product;
        $this->sku = $product->sku;
        $this->name = $product->name;
        $this->description = $product->description;

        $this->width = 4;
        $this->height = 4;
        $this->length = 10;
        $this->price = $this->calculatePrice();
    }

    /**
     * Calculates the price based on height, width and length 
     * @return int $calculatedPrice
     */
    private function calculatePrice() 
    {
        return (((($this->width + (2 * $this->height)) * 12) / 144) * 25) * $this->length;
    }

    /**
     * Life-cycle hook that will fire on each updated value from the view
     * @param string $field The fieldname that is updated
     */
    public function updated($field) 
    {
        if (in_array($field, ['width', 'height', 'length'])) {
            $this->price = $this->calculatePrice();
        }
    }

    public function render()
    {
        return view('livewire.shop.show')
                ->layout('layouts.frontend');
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72597048

复制
相关文章

相似问题

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