【问题标题】:Attribute [livewire] does not exist属性 [livewire] 不存在
【发布时间】:2020-12-31 03:25:59
【问题描述】:

我无法使用 laravel-livewire 在 Laravel 8 路由上运行我的代码。
班级在Livewire\LandingPage内。

我得到的错误是

属性 [livewire] 不存在

这是我的路线

<?php

use Illuminate\Support\Facades\Route;

Route::livewire('/' , 'LandingPage');

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

【问题讨论】:

  • 您遇到的错误是什么?请在此处添加错误日志

标签: php laravel laravel-livewire


【解决方案1】:

如果您使用 livewire v1.x,请使用此注释:

//(livewire v1.x)
Route::livewire('/post', 'LandingPage');

如果您使用的是livewire v2.0,请使用这个:

//(livewire v2.x)
Route::get('/post', \App\Http\Livewire\LandingPage::class);

【讨论】:

    【解决方案2】:

    使用 Laravel 8.29 和 LiveWire 2.4.0

    意外值异常 无效的路由操作:[App\Http\Controllers\App\Http\Livewire\Blog]。

    我认为你需要在 App\Http\Controllers 中创建一个新的控制器并将路由与这个控制器链接起来更好。在视图中使用 @liveware 到您的 LiveWire 控制器。

    Route::group(['middleware' => 'auth'], function () {
        // Only with LiveWire v1
        //Route::livewire('/blog', 'blog')->section('blog');
    
        // For LiveWire 2.
        Route::get('/blog' , 'BlogController@index');
    });
    

    App\Http\Controllers\BlogController.php

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class BlogController extends Controller
    {
        public function index(){
            return view('blog.index');
        }
    }
    

    资源/视图/博客/index.blade.php

    @livewire('blog')
    

    注意: 修复后 (https://laravel-livewire.com/docs/2.x/upgrading)

    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace) // Remove me
            ->group(base_path('routes/web.php'));
    }
    

    中间件中的路由会有问题

    Illuminate\Contracts\Container\BindingResolutionException 目标类 [Auth\LoginController] 不存在。

    【讨论】:

      【解决方案3】:

      如果您使用的是最近安装的 Laravel 8,您将拥有 Livewire V2。在这个版本中,Route::livewire()has been removed。相反,您指定一个普通的get() 路由,动作是 Livewire 组件类。

      Route::get('/' , App\Http\Livewire\LandingPage::class);
      

      【讨论】:

      • 另一个非常好的问题已经结束了...记得在你的app.blade.php 中将@yield('content'){{ $slot }} 交换,以便转换为V2。
      • 是的,这不是一个应该结束的问题。我已要求重新打开它。也就是说,您可以像以前一样使用yield/section,也可以使用插槽。取决于您使用的是局部还是组件;它们是实现相同目标的不同事物。
      • 在 laravel 8 中产量/部分对我不起作用。
      • 虽然这与这个问题无关,但请阅读laravel-livewire.com/docs/2.x/upgrading的升级手册
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2021-01-11
      • 2021-03-07
      • 2021-02-06
      • 2019-05-04
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多