首页 > 解决方案 > 我正在尝试使用 Laravel Livewire,但它不起作用。请找到以下代码并为我提供解决方案

问题描述

这是 Laravel Livewire 中的注释部分,该函数必须呈现注释,但这里显示的是硬编码注释,而不是 addNewComment 函数呈现的注释。

飞——comments.php

<div class = "flex justify-center">
  <h1>Comments</h1>
    <input type="text" placeholder="What is in Your Mind" />
    <button wire:click="addNewComment">ADD</button>

</div>

@foreach($comments as $comment)
<div class="rounded border shadow p-3 my-2">
  <p>{{$comment['id']}}</p>
    <p style="color:red;"> {{$comment['creator']}} </p>
    <p> {{$comment['created_at']}} </p>
    <p> {{$comment['body']}} </p>
</div>
@endforeach


    public $comments = [
      [
        'id' => '1',
        'body' => 'New Comment',
        'created_at' => '1 min ago',
        'creator' => 'TESTER'
      ]
    ];

      public function addNewComment()
      {
        $this->comments[] =
        [
          'id' => '111',
          'body' => $this->newComment,
          'created_at' => Carbon::now()->diffForHumans(),
          'creator' => 'TESTER'
        ];
      }

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

File-- Welcome.blade.php

<head>

  <title>Live Wire</title>
<script src="{{ asset('js/app.js') }}" defer></script>
<livewire:styles />
<livewire:scripts />

</head>

<body>

<livewire:comments />

</body>

addNewComment 函数是可点击的,但不起作用。

标签: phplaravellaravel-7

解决方案


推荐阅读