vue中的懒加载和按需加载
2024-10-20 01:22:38
懒加载
(1)定义:懒加载也叫延迟加载,即在需要的时候进行加载,随用随载。
(2)异步加载的三种表示方法:
1. resolve => require([URL], resolve),支持性好 2. () => system.import(URL) , webpack2官网上已经声明将逐渐废除,不推荐使用 3. () => import(URL), webpack2官网推荐使用,属于es7范畴,需要配合babel的syntax-dynamic-import插件使用。
(3)vue中懒加载的流程:
(4)Vue中懒加载的各种使用地方:
1.路由懒加载:
export default new Router({
routes:[
{
path: '/my',
name: 'my',
//懒加载
component: resolve => require(['../page/my/my.vue'], resolve),
},
]
})
2.组件懒加载:
components: {
historyTab:resolve => {
require(['../../component/historyTab/historyTab.vue'],resolve)
},
},
3. 全局懒加载:
Vue.component('mideaHeader', () => {
System.import('./component/header/header.vue')
})
按需加载
(1)按需加载原因:首屏优化,第三方组件库依赖过大,会给首屏加载带来很大的压力,一般解决方式是按需求引入组件。
(2)element-ui按需加载
element-ui 根据官方说明,先需要引入babel-plugin-component插件,做相关配置,然后直接在组件目录,注册全局组件。
1. 安装babel-plugin-component插件:
npm install babel-plugin-component –D
2. 配置插件,将 .babelrc修改为:
{
"presets": [
["es2015", { "modules": false }]
],
"plugins": [["component", [
{
"libraryName": "element-ui",
"styleLibraryName": "theme-default"
}
]]]
}3.引入部分组件,比如 Button和 Select,那么需要在 main.js中写入以下内容:
<code class="language-javascript">import Vue from 'vue'
import { Button, Select } from 'element-ui'
import App from './App.vue'</code>Vue.component(Button.name, Button)
Vue.component(Select.name, Select) /* 或写为
*Vue.use(Button)
*Vue.use(Select)
*/
(3)iView按需求加载:
import Checkbox from'iview/src/components/checkbox';
特别提醒:
1.按需引用仍然需要导入样式,即在 main.js 或根组件执行 import 'iview/dist/styles/iview.css';
2.按需引用是直接引用的组件库源代码,需要借助 babel进行编译,以 webpack为例:
module: {
rules: [
{test: /iview.src.*?js$/, loader: 'babel' },
{test: /\.js$/, loader: 'babel', exclude: /node_modules/ }
]
}
最新文章
- ICA和PCA
- Redis学习笔记~分布式的Pub/Sub模式
- Markdown语法手册
- TrueSkill 原理及实现
- 4 Handler相关类——Live555源码阅读(一)基本组件类
- TF-IDF算法扫盲2
- 【HDOJ】1011 Starship Troopers
- 【枚举+数学】【HDU1271】整数对 难度:五颗星
- 商人过河问题(二)java实现
- iOS自动打包并发布脚本
- 调用js中文乱码
- L2-001. 紧急救援(PAT)~最短路应用
- 远程连接身份验证错误,又找不到加密Oracle修正
- BZOJ4698 差分 + 二分 + SA
- This Product is covered by one or more of the following......的问题
- django 数据库查询 ORM
- [Go] 理解 golang 中的 nil
- Linux开机自动启动某一程序
- php生成迷宫和迷宫寻址算法实例
- 配置的好的Apache和PHP语言的环境下,如何在Apache目录下htdocs/html目录下 同时部署两个项目呢