懒加载

(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/ }
]
}

最新文章

  1. ICA和PCA
  2. Redis学习笔记~分布式的Pub/Sub模式
  3. Markdown语法手册
  4. TrueSkill 原理及实现
  5. 4 Handler相关类——Live555源码阅读(一)基本组件类
  6. TF-IDF算法扫盲2
  7. 【HDOJ】1011 Starship Troopers
  8. 【枚举+数学】【HDU1271】整数对 难度:五颗星
  9. 商人过河问题(二)java实现
  10. iOS自动打包并发布脚本
  11. 调用js中文乱码
  12. L2-001. 紧急救援(PAT)~最短路应用
  13. 远程连接身份验证错误,又找不到加密Oracle修正
  14. BZOJ4698 差分 + 二分 + SA
  15. This Product is covered by one or more of the following......的问题
  16. django 数据库查询 ORM
  17. [Go] 理解 golang 中的 nil
  18. Linux开机自动启动某一程序
  19. php生成迷宫和迷宫寻址算法实例
  20. 配置的好的Apache和PHP语言的环境下,如何在Apache目录下htdocs/html目录下 同时部署两个项目呢

热门文章

  1. git读取配置文件的顺序
  2. app开发需求文档怎么写
  3. Longest Common Prefix -最长公共前缀
  4. 024-Spring Boot 应用的打包和部署
  5. boost之string_algo
  6. 如何配置一个路径,能够既适合Linux平台,又适合Windows平台,可以从这个路径中读取文件
  7. Python之正则表达式与常用模块(Day19)
  8. LeetCode:最长公共前缀【14】
  9. 快速查找文件——Everything
  10. 024_MapReduce中的基类Mapper和基类Reducer