【问题标题】:Laravel Livewire events (e.g. click) not working. I'm using laravel 7Laravel Livewire 事件(例如点击)不起作用。我正在使用 laravel 7
【发布时间】:2021-03-10 21:23:36
【问题描述】:

我最近开始学习 Laravel Livewire。当我第一次创建 Livewire 组件时,一切正常。但是当我尝试创建一个新的时,事件和函数不再起作用。可以读取 Livewire 类的属性/变量,但我真的无法让这些功能正常工作。这是我的代码

resources/views/livewire/step.blade.php

<div>
    <button id="sampleBtn" wire:click="increment">&plus;</button>
    {{-- Doesn't change value. Still zero --}}
    <h3> {{$steps}} </h3> 
</div>

app\http\Livewire\Step.php

<?php

namespace App\Http\Livewire;
   
use Livewire\Component;
    
class Step extends Component
{
    public int $steps = 0;
        
    public function increment()
    {
        $this->steps++;
    }

    public function render()
    {
        return view('livewire.step');
    }
}

resources/views/todos

@extends('layouts.app')
@section('content')
<div class="container">
  <a href="/todo" class="btn btn-info">&larr;</a>
  <div class="col-6 offset-3 my-3">
    <div class="card mx-auto my-3">
      <div class="card-header">
        <h4 class="card-title">Create</h4>
      </div>
      <div class="card-body">
        <x-alert/>
        <form action="{{route('todo.store')}}" method="post" class="form">
          @csrf
          <div class="form-group">
            <input type="text" name="title" id="title" class="form-control" placeholder="Title">
          </div>
          <div class="form-group">
            <textarea name="description" id="description" cols="30" rows="5" class="form-control" placeholder="description"></textarea>
          </div>
          <div class="form-group">
            <input type="text" name="step" class="form-control" placeholder="add step here">
          </div>
          <button type="submit" id="createBtn" class="btn btn-block btn-primary">Add task</button>
        </form>
        {{-- I placed this outside the form because it undergoes through validation. I don't want it to be validated yet. --}}
        <div id="forLivewire">
          @livewire('step')
        </div>   
      </div>
    </div>
  </div>
</div>
@endsection

我尝试将 livewire 脚本与其他脚本一起放在 HTML 头部。它仍然不起作用。另外,我正在使用引导程序。

附:我目前正在关注 YouTube 上的教程。

编辑:我在控制台上收到“err_aborted 404”错误

【问题讨论】:

  • Welcome to SO ...您使用的是什么版本的php?
  • 我使用的是 7.4 版
  • 那么您在控制台中遇到任何错误吗?
  • 是的。我收到“net::ERR_ABORTED 404”和“未捕获的参考错误:未定义 Livewire”。我认为我的 Livewire 脚本有问题。
  • 然后修复它。

标签: php laravel laravel-livewire


【解决方案1】:

问题出在 Livewire 脚本中。我发布了资产,现在它可以工作了!

php artisan vendor:publish --force --tag=livewire:assets

【讨论】:

    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 2021-03-24
    • 2022-07-06
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2020-12-16
    相关资源
    最近更新 更多