首页 > 解决方案 > Laravel 子路由中的 Vue Router 找不到正确的组件

问题描述

我在另一个 Laravel/Vue 项目上运行了几乎相同的配置,该项目运行良好,但在主要路线上/。在这里,我将 Vue 实例分组到一个admin子路由中,我在管理员路由到PageNotFound我设置的组件之后键入的所有内容。

我在 web.php 中的路线:

Route::group(['prefix' => 'admin'], function() {
    Route::get('/{route?}', 'AdminController@index')
    ->where('route', '.*')->fallback();
});

我的 Vue 路由器设置:

const router = new VueRouter({
    mode: 'history',
    // linkActiveClass: 'active',
    linkExactActiveClass: 'exact-active',
    routes: [
        {
            path: '/', // Solution: '/admin' not just '/'
            component: Dashboard,
            meta: {
                title: 'Dashboard',
                metaTags: [
                    {
                        name: 'description',
                        content: 'The about page of our example app.'
                    },
                    {
                        property: 'og:description',
                        content: 'The about page of our example app.'
                    }
                ]
            },
        },
        {
            path: '*',
            component: PageNotFound,
            meta: {
                title: '404 - Page Not Found',
                metaTags: [
                    {
                        name: 'description',
                        content: 'The about page of our example app.'
                    },
                    {
                        property: 'og:description',
                        content: 'The about page of our example app.'
                    }
                ]
            },
        }
    ],
});

解决方案:

似乎 Vue Router 并不关心我在 Laravel 中定义的任何前缀,它会查看完整路径,因此在上面的仪表板中,我指定了/实际应该是的路径/admin

标签: laravelvue.jsvue-router

解决方案


admin 保留在 webserver 中,例如使用管理员作为前缀


推荐阅读