使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如:

import Hello from '@/components/Hello'
import Boy from '@/components/Boy'
import Girl from '@/components/Girl'

普通加载的缺点:

webpack在打包的时候会把整个路由打包成一个js文件,如果页面一多,会导致这个文件非常大,加载缓慢

1、require.ensure()实现按需加载

  • 语法:
require.ensuire(dependencies:String[],callback:function(require),errorCallback:function(error),chunkName:String)
  • vue中使用:
const List = resolve =>{ require.ensure([],()=>{ resolve(require('./list')) },'list') }

2、vue异步组件技术

在router中配置,使用这种方法可以实现按需加载,一个组件生成一个js文件

  • vue中使用:
{
path: '/home',
name: 'home',
component:resove => require(['@/components/home'],resolve)
}

3、使用动态的import()语法(推荐使用这种)

  • vue中使用:
//没有指定webpackChunkName,每个组件打包成一个js文件
const test1 = ()=>import('@/components/test1.vue')
const test2 = ()=>import('@/components/test2.vue') //指定了相同的webpackChunkName,会合并打包成y一个js文件
const test3 = ()=>import(/* webpackChunkName:'grounpTest' */ '@/components/test3.vue')
const test4 = ()=>import(/* webpackChunkName:'grounpTest' */ '@/components/test4.vue') const router = new VueRouter({
routes: [
{ path: '/test1', component: test1 },
{ path: '/test2', component: test2 },
{ path: '/test3', component: test3 },
{ path: '/test4', component: test4 }
]
})

注:

  • /* webpackChunkName: 'grounpTest' */使用命名chunk,一个特殊的注释语法来提供 chunk name (需要 Webpack > 2.4)

vue中路由按需加载的几种方式的更多相关文章

  1. vue 动态路由按需加载的三种方式

    在Vue项目中,一般使用vue-cli构建项目后,我们会在Router文件夹下面的index.js里面引入相关的路由组件,如: import Hello from '@/components/Hell ...

  2. vue项目实现路由按需加载的3种方式

    vue异步组件技术 ==== 异步加载vue-router配置路由 , 使用vue的异步组件技术 , 可以实现按需加载 .但是,这种情况下一个组件生成一个js文件 /* vue异步组件技术 */ { ...

  3. vue项目实现按需加载的3种方式:vue异步组件技术、es提案的import()、webpack提供的require.ensure()

    1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件. 举例如下: { path: '/promisedemo ...

  4. vue项目实现按需加载的3种方式

    vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载.这种方式下一个组件生成一个js文件 用例: { path: '/promisedemo', name: ' ...

  5. route按需加载的3种方式:vue异步组件、es提案的import()、webpack的require.ensure()

    1. vue异步组件技术 vue-router配置路由,使用vue的异步组件技术,可以实现按需加载. 但是,这种情况下一个组件生成一个js文件.举例如下: { path: '/promisedemo' ...

  6. vue项目按需加载的3种方式

    本文重要是路由打包优化: 原理:利用webpack对代码进行分割是懒加载的前提,懒加载就是异步调用组件,需要时候才下载. 1.vue异步组件技术 vue-router配置路由,使用vue的异步组件技术 ...

  7. vue--按需加载的3种方式(解决网页首次加载速度慢的问题)

    一.vue的异步组件加载 使用异步组件加载,打包的时候会将每个组件分开打包到不同的js文件中: {path: '/index', name: 'index', meta:{ title:'首页', r ...

  8. 【koa2基础框架封装】基于Proxy路由按需加载器和初始加载器

    我们在使用koa2做路由拦截后一般都习惯于直接将查找对应处理函数的过程映射到项目的文件夹目录,如: router.get('/test', app.controller.index.test); ap ...

  9. react16 路由按需加载、路由权限配置

    1. 路由按需加载: 不做按需加载,代码全部打包在bundle.js 文件里,首屏渲染很慢,项目文件较多,会出现1分钟加载的可能性. import React, { Component } from ...

随机推荐

  1. Python_回调函数

    import os import stat def remove_readonly(func,path): #定义回调函数 os.chmod(path,stat.S_IWRITE) #删除文件的只读文 ...

  2. JS方法:数字转换为千分位字符

    /** * 数字转为千分位字符 * @param {Number} num * @param {Number} point 保留几位小数,默认2位 */ function parseToThousan ...

  3. 队列Queue和栈

    1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...

  4. C# Linq GroupBy 分组过滤求和

    var delOrderData = orderLogList.Where(x => (x.OlStatus == 0 && x.OlUpId == null)).GroupBy ...

  5. Linux内存使用情况以及内存泄露分析之工具与方法

    <Linux C/C++ Memory Leak Detection Tool> 1. 内存使用情况分析 1.1 系统总内存分析 通过cat /proc/meminfo,可用的物理内存=M ...

  6. KVM内核文档阅读笔记

    KVM在内核中有丰富的文档,位置在Documentation/virtual/kvm/. 00-INDEX:整个目录的索引及介绍文档. api.txt:KVM用户空间API,所谓的API主要是通过io ...

  7. SpringBoot编写自定义的starter 专题

    What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...

  8. 数据库历险记(一) | MySQL这么好,为什么还有人用Oracle?

    关系型数据库(Relational DataBase Management System),简称 RDBMS.说起关系型数据库,我们脑海中会立即浮现出 Oracle.MySQL.SQLServer 等 ...

  9. 你不知道的JavaScript--Item14 使用prototype的几点注意事项

    1.在prototype上保存方法 不使用prototype进行JavaScript的编码是完全可行的,例如: function User(name, passwordHash) { this.nam ...

  10. Java8-6-Predicate接口详解

    转自https://segmentfault.com/a/1190000012256677 Predicate函数式接口的主要作用就是提供一个test方法,接受一个参数返回一个布尔类型,Predica ...