Vue打包没有包含懒加载路由

本文介绍了解决Vue项目中使用vite打包时遇到的vue-router懒加载失效的问题。通过安装并配置@babel/plugin-syntax-dynamic-import插件,实现了对动态导入语法的支持,从而解决了懒加载功能无法正常工作的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境版本

NodeJs:v14.18.1

@vue/cli 4.5.15

babel 6.26.0 (babel-core 6.26.3)

项目是若依


参考文档

【vue-router踩坑】使用vite打包项目vue-router懒加载无效】


操作步骤

  1. 安装syntax-dynamic-import插件

    npm install --save-dev @babel/plugin-syntax-dynamic-import
    
  2. 修改项目babel.config.js文件

    原来用的插件是dynamic-import-node,改成syntax-dynamic-import

    module.exports = {
      presets: [
        // https://github.com/vuejs/vue-cli/tree/master/packages/@vue/babel-preset-app
        '@vue/cli-plugin-babel/preset'
      ],
      'env': {
        'development': {
          // babel-plugin-dynamic-import-node plugin only does one thing by converting all import() to require().
          // This plugin can significantly increase the speed of hot updates, when you have a large number of pages.
          // 'plugins': ['dynamic-import-node']
          'plugins': ["syntax-dynamic-import"]
        }
      }
    }
    
  3. 打包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值