In this lesson I show how to use webpack to code split based on route in VueJS. Code splitting is a useful tool to help eliminate unused code and only load what's necessary.

Additional Resources https://router.vuejs.org/en/advanced/lazy-loading.html

After generate the project by Vue-cli, make sure those dependencies were installed already:

npm i babel-eslint babel-plugin-syntax-dynamic-import eslint-plugin-import -D

.eslintrc.js:

module.exports = {
root: true,
parserOptions: { parser: "babel-eslint" },
extends: ["plugin:vue/essential", "@vue/prettier"]
};

.babelrc:

{
"presets": ["@vue/app"],
"plugins": ["syntax-dynamic-import"]
}

routes.js:

import Vue from "vue";
import Router from "vue-router";
const Home = () => import(/* webpackChunkName: "Home" */ "./views/Home.vue");
const About = () => import(/* webpackChunkName: "About" */ "./views/About.vue"); Vue.use(Router); export default new Router({
routes: [
{
path: "/",
name: "home",
component: Home
},
{
path: "/about",
name: "about",
component: About
}
]
});

The same for lazy loading a component:

<template>
...
<h3>Modal Example</h3>
<button @click="show">Show Modal</button>
</div>
</template> <script>
const MyModal = () => import("@/components/MyModal.vue"); // lazy loading the component
export default {
name: "HelloWorld",
props: {
msg: String
},
methods: {
show () {
this.$modal.show(MyModal);
},
}
};
</script>

最新文章

  1. 应用程序框架实战十三:DDD分层架构之我见
  2. 一种基于Orleans的分布式Id生成方案
  3. 【Unity】13.3 Realtime GI示例
  4. Android 高级UI设计笔记20:RecyclerView 的详解之RecyclerView添加Item点击事件
  5. 【BZOJ】【1115】【POI2009】石子游戏KAM
  6. error while loading shared libraries: libevent-2.0.so.5解决办法
  7. UML建模——概述
  8. TCP/IP详解之:ICMP协议
  9. 周根项《一分钟速算》全集播放&amp;amp;下载地址
  10. 高质量c c++编程
  11. HDU 1596 find the safest road (最短路)
  12. 关于grub的那些事(一)
  13. Web前端Require.js
  14. windows phone 8.1开发:触控和指针事件1
  15. Docker跨主机网络——overlay
  16. SQL Server The target database (&#39;db&#39;) is in an availability group and currently does not allow read only connections. For more information about application intent, see SQL Server Books Online.
  17. 爬取json Swaggerui界面
  18. ASP.NET MVC项目中App_Code目录在程序应用
  19. 腾讯qq发邮件
  20. Windows 10 中 VMware 要求禁用 Device Guard 问题

热门文章

  1. RHEL6.5安装成功ORACLE11GR2之后,编写PROC程序出错解决方法
  2. python常见的加密方式
  3. 使用UDEV SCSI规则在Oracle Linux上配置ASM
  4. 诡异之--map clear 之后可能导致size != 0的操作
  5. Spark 概念学习系列之Spark基本概念和模型(十八)
  6. Android 微信分享不出去?四步搞定!
  7. 提高mysql千万级大数据SQL查询优化几条经验
  8. Cookie的实现
  9. Centos7安装gitlab服务器
  10. Android 动态设置 layout_centerInParent