Dynamicaly import component

Dear all,

I have to access the client side routes of my app on the server.
For this, I have created a json file containing the routes of my client side.

Some routes in the json file:

    "routes": [
            "path": "/login",
            "name": "login",
            "component": "./pages/Login",
            "meta": {
                "auth": false
            "path": "/",
            "name": "home",
            "component": "./pages/Home",
            "meta": {
                "auth": true,
                "breadcrumb": "home"
            "children": [
                    "path": "/users",
                    "components": {
                        "layout": "./pages/settings/users/Index"

Then, in my router file, I try to import the components dynamically like this:

import VueRouter from 'vue-router'
import store from "./store"

import r from './routes';


function mapRoutes(routes){
    routes.forEach(function (route) {
            route.component=() => import(route.component+'.vue');
            for (const layoutName in route.components) {
                route.components[layoutName]= () => import(route.components[layoutName]+'.vue');

const routes=r.routes;

const router = new VueRouter({
    history: true,
    mode: 'history',


I have set the Babel plugin: “@babel/plugin-syntax-dynamic-import” in my mix.js config.

But I faced an error in my browser:

What’s wrong with the dynamic import of my components?

The solution may be to find another way to access my client side routes on the server and return to the standard component’s import, like import Login from './pages/Login'

Thanks for your help.


1 Like

Hi. Can you show structure of your project?


You have the name of the component in the compent attribute but then you overwrite the name with the imported component. You assign it to an anonymous function () => import(…) and then tries to use the name again for the path to the component. Maybe it would be better to store the name/path of the component in another attribute like componentPath or something like that.