Vue.js routes serving from subdirectory

2019-03-27 07:09发布

I would like to serve my Vue.js app from a subdirectory on a staging server. For example: 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. The "home" page gets routed to the 'catch-all', and any further routes simply show the normal white-page 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/'

In local development the routes work fine. But on the staging server all static assets, the built JS and CSS etc all serve fine. 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.

1条回答
Melony?
2楼-- · 2019-03-27 07:26

The assetsPublicPath config is just for webpack assets. For vue-router, you need to set the base option in the constructor.

See docs: https://router.vuejs.org/en/api/options.html#base

查看更多
登录 后发表回答