【问题标题】:Vue-Router is not supporting dynamic import() functionVue-Router 不支持动态 import() 函数
【发布时间】:2019-07-01 09:22:34
【问题描述】:

下面的代码抛出unexpected token import

const Router = new VueRouter({
    routes: [
      { path: '', component: () => import('pages/landing/landing') }
    ]
})

如果我喜欢以下内容,它就可以工作:

import landing from 'pages/landing/landing';
const LandingComp = Vue.component('search-product', landing);

const Router = new VueRouter({
    routes: [
        { path: '', component: LandingComp }
    ]
})

但我不想一开始就加载所有组件。我想在需要时动态加载。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:

    通过适当的 babel 配置启用动态导入。你必须正确配置 babel。见here

    【讨论】:

      【解决方案2】:
      import LandingComp = () => { 
          import landing from 'pages/landing/landing'
          Vue.component('search-product', landing)
      }
      
      const Router = new VueRouter({
          routes: [
              { path: '', component: LandingComp }
          ]
      })
      

      你可以试试

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-12
        • 2020-04-22
        • 2018-04-01
        • 2019-06-05
        • 2020-07-19
        • 2020-12-09
        • 2023-02-22
        • 2017-03-15
        相关资源
        最近更新 更多