代码之家  ›  专栏  ›  技术社区  ›  Slaveworx Ivo Jerkovic

我的服务器将我的路由路径假定为目录(Vue路由器4-Vue 3)

  •  0
  • Slaveworx Ivo Jerkovic  · 技术社区  · 4 年前

    npm run build 把我的第一个大Vue3应用程序带到真实的服务器测试驱动器上。

    所以我得到了这些文件,上传到我的VPS中,然后惊奇地发现:只有当我通过点击导航时,服务器才能识别路由路径。

    https://next.router.vuejs.org/guide/essentials/history-mode.html 给我的 .htaccess 文件(我在Centos 7上使用apache 但问题仍然存在,唯一的区别是它不是抛出错误,而是陷入了 index.html

    我正在定义 main.js 文件,如下所示:

    import { createRouter, createWebHistory } from "vue-router"
    import Page from "./views/Page.vue"
    import Content from "./views/Content.vue"
    import BaseSection from "./components/BaseSection.vue"
    
    //ROUTER
    const router = createRouter({
        history: createWebHistory(),
        routes: [{
                path: "/",
                name: "Home",
                component: Page,
                meta: {
                    title: 'Home'
                }
            },
            {
                path: "/docs/1.0/:title",
                component: Content,
                name: "docs"
            }
        ],
        scrollBehavior(to) {
            if (to.hash) {
                return {
                    el: to.hash,
                    behavior: 'smooth',
                }
            }
        }
    });
    
    app.use(router);
    
    

    谢谢您。

    当做, T。

    1 回复  |  直到 4 年前
        1
  •  1
  •   Dan    4 年前

    当从域根目录以外的其他位置提供应用程序时,通常会发生空白页错误。在这种情况下,必须相应地配置Vue CLI。Modfiy公司 vue.config.js版

    module.exports = {
      publicPath: process.env.NODE_ENV === 'production'
        ? '/docs/1.0/'  // This is whatever your path from the root is
        : '/'
    }
    

    你可以读更多关于 publicPath here

    确保您的 .htaccess 正在