【问题标题】:Laravel Livewire wire:click not trigger function definedLaravel Livewire 线:单击未定义触发功能
【发布时间】:2021-06-21 14:43:45
【问题描述】:

我正在网页中构建搜索栏。理想情况下,用户将在搜索字段中输入搜索文本,然后如果找到记录,则搜索结果表将显示并显示找到的记录。我正在使用 Laravel Livewire 来实现这个功能,但是,我遇到了 wire:click 没有触发事件的问题,需要任何帮助!

这是我的刀片文件 (resources/livewire/dashboard.blade.php) 包含搜索栏:

<form>
    <label for="searchText" class="block text-xx font-medium text-gray-700">Search Users</label>
    <div class="mt-1 flex rounded-md shadow-sm">
        <div class="relative flex items-stretch flex-grow focus-within:z-10">
            <input type="text" name="searchText" id="searchText"
                       class="focus:ring-indigo-500 focus:border-indigo-500 block w-full rounded-none rounded-l-md pl-10 sm:text-sm border-gray-300" placeholder="User ID / Email Address / Mobile Number"
                       wire:model="searchText">
        </div>
        <button wire:click="search()" class="-ml-px relative inline-flex items-center space-x-2 px-4 py-2 border border-gray-300 text-sm font-medium rounded-r-md text-gray-700 bg-gray-50 hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500">
            Search
        </button>
    </div>
</form>

这是在 App/Http/Livewire/Dashboard.php 文件中定义的操作

<?php

namespace App\Http\Livewire;

use Illuminate\Support\Facades\Http;
use Livewire\Component;

class Dashboard extends Component
{
    public $stats, $searchText;
    public $showResultsTable = false;
    protected $accountAPIRootURL = 'https://example.com/api/v2/';

    public function render()
    {
        $response = Http::withHeaders([
            'Accept' => 'application/json'
        ])->get($this->accountAPIRootURL . 'statistics/overview');

        if ($response->successful()) {
            $stats = $response['data'];
        } else {
            $stats = [
                'total_users' => 0,
                'new_users' => 0,
                'invitations' => 0,
                'new_invitations' => 0,
                'requests' => 0,
                'new_requests' => 0
            ];
        }

        $this->stats = $stats;
        $this->searchText = '';
        return view('livewire.dashboard');
    }

    public function search()
    {
        $response = Http::withHeaders([
            'Accept' => 'application'
        ])->get($this->accountAPIRootURL . 'admin/search', [
            'searchText' => $this->searchText
        ]);

        if ($response->successful()) {
            $this->showResultsTable = true;
            $this->searchText = '';
        }
    }
}

这是我的 template.blade.php 文件,其中调用了 @livewire 组件

@extends('layouts.app')

@section('content')
   @livewire('dashboard')
@endsection

我现在不太担心显示结果表,因为当我单击刀片中的“搜索”按钮时,似乎没有触发 search() 函数。我怎么知道,我在 search() 函数中放了一个 dd() 并且它没有被执行。

我将不胜感激!

【问题讨论】:

  • 问题已经修复

标签: php laravel laravel-livewire livewires


【解决方案1】:

你不需要使用括号,wire:click="search"

更新:在 livewire 中处理表单时尝试这种不同的语法

<form wire:submit.prevent="search">
    //.....
    <div class="mt-1 flex rounded-md shadow-sm">
        //.....
        <button class="-ml-px relative inline-flex items-center space-x-2 px-4 py-2 border border-gray-300 text-sm font-medium rounded-r-md text-gray-700 bg-gray-50 hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500">
            Search
        </button>
    </div>
</form>

【讨论】:

  • 确保您的刀片正在加载@livewire 资产,如果请求被发送到后端,请检查 DevTool 控制台是否有任何问题和网络选项卡
  • 我在 layouts/app.blade.php 中有 @livewireStyles@livewireScripts,在 app.blade.php 中有一个部分 yield('content'),还有另一个名为 template 的文件.blade.php 来实现内容部分,在@section('content') 内,我有@livewire('dashboard'),其中包含上面粘贴在问题中的代码
  • 那么,当您检查仪表板刀片 html 时,您可以看到加载的资产吗?控制台有问题吗?例如,将按钮放在 div 中
  • 我确实看到了 app.css 和 app.js 的加载,我还看到了 livewire.js 的加载,还有我需要寻找的任何东西。当你说在
    中添加一个按钮时,我应该对按钮做些什么以及我应该看到什么结果?
  • 查看答案更新,尝试不同的表单句柄功能。让我知道结果
猜你喜欢
相关资源
最近更新 更多
热门标签