[Vue] Code split by route in VueJS
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>
[Vue] Code split by route in VueJS的更多相关文章
- vue vue-route 传参 $route.params
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- vue中$router以及$route的使用
路由基本概念 route,它是一条路由. { path: '/home', component: Home } routes,是一组路由. const routes = [ { path: '/hom ...
- vue中router以及route的使用
路由基本概念 route,它是一条路由. { path: '/home', component: Home } routes,是一组路由. const routes = [ { path: '/hom ...
- vue路由对象($route)参数简介
路由对象在使用了 vue-router 的应用中,路由对象会被注入每个组件中,赋值为 this.$route ,并且当路由切换时,路由对象会被更新. so , 路由对象暴露了以下属性: 1.$rout ...
- vue中$router和$route的区别
$router是VueRouter的实例,在script标签中想要导航到不同的URL,使用$router.push方法. 返回上一个历史history用$router.to(-1) $route为当前 ...
- [Vue] Lazy Load a Route by using the Dynamic Import in Vue.js
By default, vue-router doesn’t lazy load the routes unless you tell it to do it. Lazy loading of the ...
- vue中$router与$route的区别
$.router是VueRouter的实例,相当于一个全局的路由器对象.包含很多属性和子对象,例如history对象 $.route表示当前正在跳转的路由对象.可以通过$.route获取到name,p ...
- vue中$router 与 $route区别
vue-router中经常会操作的两个对象\(route和\)router两个. 1.$route对象 $route对象表示当前的路由信息,包含了当前 URL 解析得到的信息.包含当前的路径,参数,q ...
- 【Vue.js】简单说下vuejs中v-model自定义使用姿势
vue.js中有个v-model的语法,可以实现双向绑定. 起初刚看到的时候,觉得很神奇.后面随着对vue.js的熟悉.发现这个其实是vue官方给我们实现的一个语法糖. 使用v-model的时候,vu ...
随机推荐
- Netty(2) - HelloWorld
Netty:作用场景. 1)Netty可以基于socket实现远程过程调用(RPC). 2)Netty可以基于WebSocket实现长连接. 3)Netty可以实现Http的服务器,类似于Jetty, ...
- [转]通过Net Manager 配置Oracle 11g本地监听服务(listener service)
本文转自:http://blog.csdn.net/mozart_cai/article/details/8596504 [Target] 通过ip address 监听orcl服务,而不是通过loc ...
- MVC系列学习(六)-Razor语法
注:本次代码加了样式,样式如下 <style> div { border: 1px solid red; margin: 10px auto; ...
- Quartz中时间参数说明 即Cron表达式
Cron表达式 Quartz使用类似于Linux下的Cron表达式定义时间规则,Cron表达式由6或7个由空格分隔的时间字段组成,如表1所示: 表1 Cron表达式时间字段 位置 时间域名 允许值 允 ...
- Python初学1
windows版python下载: https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ 安装时勾选pip和Add python.exe to Path. w ...
- Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别?
Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别? Java 中访问数据库的步骤 1)注册驱动: 2)建立连接: 3)创建Statement: 4)执 ...
- python笔记之发送邮件
发送邮件前提:开启邮箱授权码 一.开启授权码(以163邮箱为例) 1.登录163邮箱,点击设置--POP3/SMTP/IMAP,出现设置界面 2. 开启SMTP服务且可以查询SMTP的host地址 ...
- Python 之selenium+phantomJS斗鱼抓取案例
from selenium import webdriver from bs4 import BeautifulSoup import time if __name__ == '__main__': ...
- ARM异常中断返回的几种情况
在学习韦老师视频中中断异常部分时候,对于发生中断时需要执行的#保存异现场 #恢复现场 中的“返回”弄不清楚,查阅网络文章后,发现一篇概述我觉得我能理解的一篇如下: 重要基础知识:R15(PC)总是 ...
- CSS 选择器 知识点
<html> <head> <style type="text/css"> h1 > strong { /*子元素选择器 只选择自己 的子 ...