语法错误,意外的“@”,期待变量(T_VARIABLE)或“{”或“$”

syntax error, unexpected '@', expecting variable (T_VARIABLE) or '{' or '$'

我正在使用 php 7.4.27laravel 框架 8.83.7 并安装了 LaravelJetstream登录&注册即可显示。注册后,然后在Laravel中出现错误通知: 语法错误,意外的“@”,应为变量 (T_VARIABLE) 或“{”或“$” 在代码第 12 行中:

<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'jetstream::components.dropdown-link','data' => ['href' => $href,'@click.prevent' => $@clickPrevent]]); ?>

我不会更新到php 8.我该怎么办?

=====已编辑=====

composer.json 文件:

{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "php": "^7.3|^8.0",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "laravel/framework": "^8.75",
    "laravel/jetstream": "^2.7",
    "laravel/sanctum": "^2.11",
    "laravel/tinker": "^2.5",
    "livewire/livewire": "^2.5"
},
"require-dev": {
    "facade/ignition": "^2.5",
    "fakerphp/faker": "^1.9.1",
    "laravel/sail": "^1.0.1",
    "mockery/mockery": "^1.4.4",
    "nunomaduro/collision": "^5.10",
    "phpunit/phpunit": "^9.5.10"
},
"autoload": {
    "psr-4": {
        "App\": "app/",
        "Database\Factories\": "database/factories/",
        "Database\Seeders\": "database/seeders/"
    }
},
"autoload-dev": {
    "psr-4": {
        "Tests\": "tests/"
    }
},
"scripts": {
    "post-autoload-dump": [
        "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover --ansi"
    ],
    "post-update-cmd": [
        "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
    ],
    "post-root-package-install": [
        "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "@php artisan key:generate --ansi"
    ]
},
"extra": {
    "laravel": {
        "dont-discover": []
    }
},
"config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true

}

解释:

问题是由于 storage/framework/views 下编译的视图版本无法评估 $@clickPrevent

<?php $component = $__env->getContainer()->make(Illuminate\View\AnonymousComponent::class, ['view' => 'jetstream::components.dropdown-link','data' => ['href' => $href,'@click.prevent' => $@clickPrevent]]); ?>

这与您当前的 PHP 版本无关。

解决方案:

我设法在本地重现了这个问题。 我是这样解决的。

步骤:

  1. 发布 Jetstream Livewire 组件。

    命令:

    php artisan vendor:publish --tag=jetstream-views

  2. 打开文件resources/views/vendor/jetstream/components/switchable-team.blade.php

    在文件内容中,

    而不是:

     <x-dynamic-component :component="$component" href="#" @click.prevent="$root.submit();"> ❌
    

    使用这个:

     <x-dynamic-component :component="$component" href="#"> ✅
    

免责声明:

请注意,这是一个临时解决方案。一切对我来说都很好。正如所感叹的那样,恐怕它可能会影响一些未知的功能。我在这里针对图书馆提出了一个问题:

switchable-team.blade.php compiled Jetstream component throwing an error: syntax error, unexpected token "@", expecting variable or "{" or "$"


第 10 次更新/April/2022:

另一个 “更稳定” 解决方案 @fabpl 在打开的 Github 问题上陈述:

I think it's the same bug like [2.x] Fix parse error caused by alpine shorthand on dynamic-component #1032

Try to replace @click with x-on:click

附录:

问题似乎是 introduced in laravel/jetstream v2.7.2. More specifically Use Alpine's @click.prevent for switching teams

好消息是这 resolved beginning from laravel/jetstream v2.7.3 onwards. More specifically [2.x] Fix parse error caused by alpine shorthand on dynamic-component

I just tagged v2.7.3 which should fix this. Thanks all

因此,您只需将 Jetstream 版本升级到 v2.7.3 即可获得修复。即:

命令:

composer update laravel/jetstream:2.7.3

示例输出:

PS C:\Users\Ivan\Documents\SteveContents\GITHUB\Laravel-8.83.7\laravel-8.83.7> composer update laravel/jetstream:2.7.3
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading laravel/jetstream (v2.7.2 => v2.7.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Downloading laravel/jetstream (v2.7.3)
  - Downloading laravel/jetstream (v2.7.3)
  - Downloading laravel/jetstream (v2.7.3)
  - Downloading laravel/jetstream (v2.7.3)
  - Upgrading laravel/jetstream (v2.7.2 => v2.7.3): Extracting archive
...