我正在尝试在 laravel 10 中将 select2 与 livewire 3 一起使用。 但是当我尝试在 resources/views/layouts/app.blade.php 中编写脚本时,它显示以下错误, “()=> 表达式语句不是赋值或调用”
下面是我使用过的代码。关于 window.livewire.on('data-change-event'
导致此问题的原因) 的任何建议**resources/views/layouts/app.blade.php
**<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.bunny.net">
<link href="https://fonts.bunny.net/css?family=Nunito" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<!-- Scripts -->
@vite(['resources/sass/app.scss', 'resources/js/app.js'])
</head>
<body>
<div id="app">
<main class="py-4">
@yield('content')
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<script>
$(document).ready(function() {
// Select2 Multiple
$('.select2-multiple').select2({
placeholder: "Select",
allowClear: true
});
window.livewire.on('data-change-event',()=>{
$('.select2-multiple').select2({
placeholder: "Select",
allowClear: true
});
});
});
</script>
</body>
</html>
**resources/views/components/layouts/app.blade.php
**
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ $title ?? 'Page Title' }}</title>
</head>
<body>
{{ $slot }}
</body>
</html>
**resources/views/create-article.blade.php
**
@extends('layouts.app')
@section('content')
@livewire('create-article')
@endsection
**resources/views/livewire/create-article.blade.php
**
<div>
<div class="container">
<h1>Create a new article</h1>
<form wire:submit.prevent="articleFormValidation">
<div class="mb-3 mt-3">
<label for="title" class="form-label">Title:</label>
<input type="text" class="form-control" id="title" placeholder="Enter Title" wire:model.live="title">
@error('title'){{$message}} @enderror
</div>
<div class="mb-3 mt-3">
<label for="description">Description:</label>
<textarea class="form-control" rows="5" id="comment" name="description"></textarea>
</div>
<div class="form-group mb-3">
<label for="select2Multiple">Tags</label>
<select class="select2-multiple form-control" name="tags[]" multiple="multiple"
id="select2Multiple">
<option value="tag1">tag1</option>
<option value="tag2">tag2</option>
<option value="tag3">tag3</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
**CreateArticle.php
**
<?php
namespace App\Livewire;
use Livewire\Component;
class CreateArticle extends Component
{
public $title;
public $description;
public $tags=[];
protected $rules=[
'title'=> 'required',
'description'=> 'required',
];
public function articleFormValidation()
{
$this->validate();
}
public function render()
{
return view('livewire.create-article');
}
public function hydrate()
{
$this->emit('data-change-event');
}
}
还是这个功能
public function hydrate()
{
$this->emit('data-change-event');
}
需要。