我目前正在构建一个Laravel应用程序,其中两个仪表板用于管理员,第二个用于用户.用户后端应该在域/user-dashboard/下运行./dashboard下的后台管理员.

在Vue中,我使用Vue路径,并且我的base url有问题.因为Vue路由不会将Vue routes链接添加到我的base url.它总是以"/"开头.我怎么才能修好它呢?

这是我的web.php个、router.jsvite.config.js

web.php

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

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

routes/index.js

import { createWebHistory, createRouter } from "vue-router";
import Home from "../views/HomeView.vue";
import Profile from '../views/ProfileView.vue';
import NotFound from '../views/NotFoundView.vue';

const routes = [
    {
        path: "/",
        name: "Home",
        component: Home,
    },
    {
        path: "/profile",
        name: "Profile",
        component: Profile,
    },
    {
        path: "/:catchAll(.*)",
        component: NotFound,
    },  
];


const router = createRouter({
    history: createWebHistory(),
    base: '/user-dashboard/',
    routes,
});

export default router;

vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from "@vitejs/plugin-vue"

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});

推荐答案

您可以在createWebHistory函数中的routes/index.js文件中传递基本路径.按如下方式更改您的文件:

const router = createRouter({
    history: createWebHistory( '/user-dashboard/'),
    routes,
});

如果您将常量(例如:VITE_BASE_VUE_PATH="/user-dashboard")添加到您的.env文件中,然后传递import.meta.env.VITE_VUE_PATH,则在可定制性方面会更出色.喜欢:

const router = createRouter({
    history: createWebHistory( import.meta.env.VITE_VUE_PATH ),
    routes,
});

然后,您必须调整Laravel路由,以便您可以拦截通过/user-dashboard运行的所有请求.

Route::group(['prefix' => 'user-dashboard'], function() {
  Route::get('/', function () {
      return view('userdashboard');
  });

  Route::get('{any}', function () {
      return view('userdashboard');
  });
})->middleware(['auth', 'verified'])->name('user.dashboard');

这应该行得通!

Laravel相关问答推荐

Laravel:在 PHPUnit 的覆盖率报告中包含文件夹(如果在 app/ 文件夹之外)?

Laravel 获取具有多对多关系的中间表数据

Laravel Eloquent:组合列(或自定义属性)上的 Where 子句

如何在生产中的 laravel 应用程序中获取标头请求值?

如何在 laravel 5.2 中显示 500 内部服务器错误页面?

上传时Laravel正在旋转图像

Laravel belongsToMany 关系在两个表上定义本地键

Laravel 4:验证前修剪输入的最佳实践

如何判断是否在laravel中设置了cookie?

php Laravel-遇到格式不正确的数值(在字符串上)

如何使用外部图像生成 html div 的 screenshot截图?

当表中不存在列时如何将 paginate() 与 having() 子句一起使用

在 laravel 6.0 中未定义命令ui

Laravel 5.4 有时验证规则不起作用

Filesystem.php 中的 ErrorException

在 Laravel 中,如何获取 *only* POST 参数?

Laravel 迁移 - 删除列

在我的编辑器中管理上传文件的最佳方式?

Laravel 4 控制器模板/Blade - 正确的方法?

Laravel 5.3 - 用户Collection 的单一通知(关注者)