简体   繁体   English

Vue.js 路由从子目录提供服务

[英]Vue.js routes serving from subdirectory

I would like to serve my Vue.js app from a subdirectory on a staging server.我想从登台服务器上的子目录提供我的 Vue.js 应用程序。 For example: http://url.com/subdir/app/例如: http : //url.com/subdir/app/

Right now if I do this and set up the build config assetsPublicPath to serve from that folder, all the assets are served fine but my app does not get routed correctly.现在,如果我这样做并设置构建配置 assetsPublicPath 以从该文件夹提供服务,则所有资产都可以正常提供,但我的应用程序无法正确路由。 The "home" page gets routed to the 'catch-all', and any further routes simply show the normal white-page 404. “主页”页面被路由到“全能”页面,任何进一步的路由只显示正常的白页 404。

Here is my router:这是我的路由器:

export default new Router({
    mode: 'history',
    routes: [
    {
        path: '/',
        component: ContentView,
        children: [
            {
                path: '/',
                name: 'DataTable',
                component: DataTable,
                meta: { requiresAuth: true }
            },

            // ===================================
            //  Login
            // ===================================
            {
                path: '/login',
                name: 'AppLogin',
                component: AppLogin,
                meta: { checkAlreadyLoggedIn: true }
            },
            {
                path: '/logout',
                name: 'AppLogout',
                component: AppLogout,
                meta: { requiresAuth: true }
            }
        ]
    },
    {
        path: '*',
        component: ContentView,
        children: [
            {
                path: '/',
                name: 'NotFound',
                component: NotFound
            }
        ]
    }
]})

And here is the necessary config/index.js changes: assetsPublicPath: '/subdir/app/'这是必要的 config/index.js 更改: assetsPublicPath: '/subdir/app/'

In local development the routes work fine.在本地开发中,路线工作正常。 But on the staging server all static assets, the built JS and CSS etc all serve fine.但是在临时服务器上,所有静态资产、构建的 JS 和 CSS 等都可以正常运行。 However the home route shows the catch-all.然而,主场路线显示了一切。 I am assuming it's because my routes are not set up correctly, or because I need to do something to serve from a subdirectory in production.我假设这是因为我的路线设置不正确,或者因为我需要做一些事情来从生产中的子目录提供服务。

The assetsPublicPath config is just for webpack assets. assetsPublicPath配置仅适用于 webpack 资产。 For vue-router, you need to set the base option in the constructor.对于 vue-router,需要在构造函数中设置base选项。

See docs: https://router.vuejs.org/en/api/options.html#base请参阅文档: https : //router.vuejs.org/en/api/options.html#base

I have been able to solve this, using the base property of vue-route .我已经能够解决这个问题,使用vue-routebase属性。 In the case of the example it would look like this:在示例的情况下,它看起来像这样:

export default new Router({
    mode: 'history',
    base: '/subdir/app/',
    routes: [
    {
        path: '/',
        component: ContentView,
        children: [

My question now is how can I make this subdirectory dynamically.我现在的问题是如何动态创建这个子目录。 Imagine a single server, and 2 different urls pointing to this server.想象一个单一的服务器,有 2 个不同的 url 指向这个服务器。

www.dominio / app1
www.dominio / app2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM