Vue路由器延迟加载路由不起作用

时间:2020-06-19 21:37:43

标签: vuejs2 vue-router

我使用了Vue CLI并构建了一个spa。

router.js中,我尝试:

export default new Router({
  mode: 'history',
  routes: [
    { path: "",
      name: "home", 
      component: Home
    },
    { 
      path: "/about",
      name: "about",
      component: () => {
       import ("./views/About")
    } 
    },
    { 
      path: "/contact",
      name: "contact",
      component: () => {
       import ("./views/Contact")
      } 
    }
  ]
})

但是我的路由器链接和路由不再起作用了吗?

从这里我read是否必须安装syntax-dynamic-import?但是,如何添加该插件webpack.config.js?我该如何工作?

1 个答案:

答案 0 :(得分:1)

尝试从函数返回导入:

export default new Router({
  mode: 'history',
  routes: [
    { path: "",
      name: "home", 
      component: () => import("./views/Home")
    },
    { 
      path: "/about",
      name: "about",
      component: () => import("./views/About") 
    },
    { 
      path: "/contact",
      name: "contact",
      component: () => import("./views/Contact")
    }
  ]
})