[livewire]属性不存在

16

我在使用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');

你遇到了什么错误?请在此处添加错误日志。 - fahim152
4个回答

35
如果您正在使用最新的Laravel 8安装程序,则将拥有Livewire V2。在此版本中,已删除Route::livewire()。相反,您需要指定一个普通的get()路由,并将操作设置为Livewire组件类。
Route::get('/' , App\Http\Livewire\LandingPage::class);

6
在 Stack Overflow 上,又有一个完全合适的问题被关闭了。记得将 @yield('content') 替换为 {{ $slot }},以便进行到 V2 的转换,在你的 app.blade.php 文件中做出这个修改。 - sorrell
是的,这个问题本不应该被关闭。我已经要求重新打开它。话说回来,你可以像以前一样使用 yield/section 或者插槽。这取决于你是使用部分视图还是组件;它们是实现相同目标的不同方法。 - Qirel
1
在 Laravel 8 中,yield/section 对我无效。 - mbouzahir
1
虽然这与本问题无关,但请阅读 https://laravel-livewire.com/docs/2.x/upgrading 上的升级手册。 - Qirel

8
如果您使用livewire v1.x,请使用以下注释:
//(livewire v1.x)
Route::livewire('/post', 'LandingPage');

如果您正在使用livewire v2.0,请使用这个:
//(livewire v2.x)
Route::get('/post', \App\Http\Livewire\LandingPage::class);

感谢stic-lab,这仍然适用于Laravel 2.5。 - Two

0

从错误信息来看,似乎您没有设置认证系统:

Route::group(['middleware' => 'auth'], function () {
    // Only with LiveWire v1
    //Route::livewire('/blog', 'blog')->section('blog');

    // For LiveWire 2.
    Route::get('/blog' , 'BlogController@index');
});

您正在调用 auth 中间件,错误提示没有找到位于 Auth\LoginController 的 LoginController。
您是否设置了任何 auth 脚手架?
没意识到这是如此古老的帖子。

-2

使用 Laravel 8.29 和 LiveWire 2.4.0

UnexpectedValueException 无效的路由操作:[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');
    }
}

resources/views/blog/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]不存在。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接