[Vue] Code split by route in VueJS
2024-08-28 15:20:32
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>
最新文章
- 应用程序框架实战十三:DDD分层架构之我见
- 一种基于Orleans的分布式Id生成方案
- 【Unity】13.3 Realtime GI示例
- Android 高级UI设计笔记20:RecyclerView 的详解之RecyclerView添加Item点击事件
- 【BZOJ】【1115】【POI2009】石子游戏KAM
- error while loading shared libraries: libevent-2.0.so.5解决办法
- UML建模——概述
- TCP/IP详解之:ICMP协议
- 周根项《一分钟速算》全集播放&;amp;下载地址
- 高质量c c++编程
- HDU 1596 find the safest road (最短路)
- 关于grub的那些事(一)
- Web前端Require.js
- windows phone 8.1开发:触控和指针事件1
- Docker跨主机网络——overlay
- 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.
- 爬取json Swaggerui界面
- ASP.NET MVC项目中App_Code目录在程序应用
- 腾讯qq发邮件
- Windows 10 中 VMware 要求禁用 Device Guard 问题