link2740 link2741 link2742 link2743 link2744 link2745 link2746 link2747 link2748 link2749 link2750 link2751 link2752 link2753 link2754 link2755 link2756 link2757 link2758 link2759 link2760 link2761 link2762 link2763 link2764 link2765 link2766 link2767 link2768 link2769 link2770 link2771 link2772 link2773 link2774 link2775 link2776 link2777 link2778 link2779 link2780 link2781 link2782 link2783 link2784 link2785 link2786 link2787 link2788 link2789 link2790 link2791 link2792 link2793 link2794 link2795 link2796 link2797 link2798 link2799 link2800 link2801 link2802 link2803 link2804 link2805 link2806 link2807 link2808 link2809 link2810 link2811 link2812 link2813 link2814 link2815 link2816 link2817 link2818 link2819 link2820 link2821 link2822 link2823 link2824 link2825 link2826 link2827 link2828 link2829 link2830 link2831 link2832 link2833 link2834 link2835 link2836 link2837 link2838 link2839 link2840 link2841 link2842 link2843 link2844 link2845 link2846 link2847 link2848 link2849 link2850 link2851 link2852 link2853 link2854 link2855 link2856 link2857 link2858 link2859 link2860 link2861 link2862 link2863 link2864 link2865 link2866 link2867 link2868 link2869 link2870 link2871 link2872 link2873 link2874 link2875 link2876

[Vue.js] Alternative for component () => import in VueJs routing

I downloaded a template online to better understand VueJs and also create a web app. However there is a problem with routing. There is a function in my router’s index.js that imports a path. The import syntax seems to be buggy due to some webpack issues. I tried a lot of different things but couldn’t fix the bug so to find a workaround for that import syntax

This is my code for router’s index.js

import vue.js from ‘vue’
import VueAnalytics from ‘vue-analytics’
import Router from ‘vue-router’
import Meta from ‘vue-meta’

// Routes
import paths from ‘./paths’
// import views from ‘./views’

function route (path, view, name) {
return {
name: name || view,
path,
component: () => import(
`../views/${view}.vue`
)
}
}

Vue.use(Router)

// Create a new router
const router = new Router({
mode: ‘history’,
routes: paths.map(path => route(path.path, path.view, path.name)).concat([
{ path: ‘*‘, redirect: ‘/home’ }
]),
scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
}
if (to.hash) {
return { selector: to.hash }
}
return { x: 0, y: 0 }
}
})

Vue.use(Meta)

// Bootstrap Analytics
// Set in .env
// https://github.com/MatteoGabriele/vue-analytics
if (process.env.GOOGLE_ANALYTICS) {
Vue.use(VueAnalytics, {
id: process.env.GOOGLE_ANALYTICS,
router,
autoTracking: {
page: process.env.NODE_ENV !== ‘development’
}
})
}

export default router

When i try to build it, I get an error saying:

ERROR in ./src/router/index.js
Module build failed: SyntaxError: C:/djangoProjects/martin - Copy/martin/src/router/index.js: Unexpected token (15:21)

The syntax error is on line (15:21), in the route function on the component: () => import( line and exactly on import. Fixing this issue is a pain so I was wondering if there is a workaround for it without using the import syntax?

Solution :

If I remember correctly you’ll need a plugin for babel that can handle dynamic imports.

Check out:
https://babeljs.io/docs/en/babel-plugin-syntax-dynamic-import

Run npm install @babel/plugin-syntax-dynamic-import
Create or open .babelrc

{
“plugins”: [“@babel/plugin-syntax-dynamic-import”]
}