webpack2 的中 System.import 方法将被弃用, 推荐改成以下写法:

https://www.mmxiaowu.com/article/5848239bd4352863efb55469

  1. 正常写法
const centerHome = resolve => require(['../pages/home/center-home.vue'], resolve)
const centerInfo = resolve => require(['../pages/home/center-info.vue'], resolve)
const centerShop = resolve => require(['../pages/home/center-shop.vue'], resolve) const router = new VueRouter({
mode: 'hash',
base: __dirname,
scrollBehavior,
routes: [
{ name:'center-home', path: '/center/home', component: centerHome },
{ name:'center-info', path: '/center/info', component: centerInfo },
{ name:'center-shop', path: '/center/shop', component: centerShop },
]
})
  1. 支持分组的写法
const centerHome = r => require.ensure([], () => r(require('../pages/home/center-home.vue')), 'center')
const centerInfo = r => require.ensure([], () => r(require('../pages/home/center-info.vue')), 'center')
const centerShop = r => require.ensure([], () => r(require('../pages/home/center-shop.vue')), 'center') const router = new VueRouter({
mode: 'hash',
base: __dirname,
scrollBehavior,
routes: [
{ name:'center-home', path: '/center/home', component: centerHome },
{ name:'center-info', path: '/center/info', component: centerInfo },
{ name:'center-shop', path: '/center/shop', component: centerShop },
]
})

昨天将一个vue 1.0的手机端项目升级全面升级到2.0 (webpack, vue, vue-router, vuex), 发现懒加载不能用了

查资料后发现, 主要原因是webpack的升级

在webpack1时代, 路由配置里, 懒加载是这么写的:

// vue1
'/group/log': {
name: 'grouplog',
component(resolve) {
require(['./components/group/group-log.vue'], resolve)
}
}
// vue2
const Foo = resolve => require(['./Foo.vue'], resolve)
const router = new VueRouter({
routes: [
{ path: '/foo', component: Foo }
]
})
`

但是到webpack2后, webpack修改了api, (准确来说是: webpack2 已经支持原生的 ES6 的模块加载器了)所以您需要这么写: (相关资料)

// vue1
const Question = () => System.import('./components/question/index.vue')
// 中间代码省略
'/group/log': {
name: 'grouplog',
component: Question
}
// vue2
const Question = () => System.import('./components/question/index.vue')
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/question/:id', component: Question },
]
})

(转)Webpack2 + Vue2 + Vue-Router2 如何实现懒加载?的更多相关文章

  1. vue + vue-lazyload 实现图片懒加载

    1.安装 npm i vue-lazyload -S 2.配置 main.js /***图片模板等懒加载 start ***/ import VueLazyload from 'vue-lazyloa ...

  2. Vue实现一个图片懒加载插件(转载)

    Vue是可以自定义指令的,最近学习过程中遇见了一个需要图片懒加载的功能,最后参考了别人的代码和思路自己重新写了一遍.以下将详细介绍如何实现自定义指令v-lazyload. 先看如何使用这个指令: &l ...

  3. vue实现menu菜单懒加载

    本文将在vue+element ui项目中简单实现menu菜单的懒加载. 最近接到这样的需求:菜单的选项不要固定的,而是下一级菜单选项需要根据上级菜单调接口来获取.what? 这不就是懒加载吗?翻了一 ...

  4. vue.js 二 路由懒加载

    当项目小的时候,我没考虑要去找这个得解决方案,也幸好现在几乎能迁移的项目都整合在了一个vue的项目里面 才发现编译后的vendor.js变得异常的大,而且几乎在项目每一个页面都需要加载这一个js,项目 ...

  5. 在webpack中使用Code Splitting--代码分割来实现vue中的懒加载

    当Vue应用程序越来越大,使用Webpack的代码分割来懒加载组件,路由或者Vuex模块, 只有在需要时候才加载代码. 我们可以在Vue应用程序中在三个不同层级应用懒加载和代码分割: 组件,也称为异步 ...

  6. vue中的懒加载和按需加载

    懒加载 (1)定义:懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. (2)异步加载的三种表示方法: 1. resolve => require([URL], resolve),支持性好 ...

  7. Vue代码分割懒加载的实现方法

    什么是懒加载 懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载 在单页应用中,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要加载的内容过多 ...

  8. VUE图片懒加载-vue lazyload插件的简单使用

    序:vue项目时候,我们要对图片进行懒加载处理,这个开发项目中就不需要自己去写了,因为比较方便使用vue lazyload进行处理,高效率开发 一. vue lazyload插件: 插件地址:http ...

  9. Vue Router的懒加载路径

    单页应用产出的入口chunk大小随着业务的复杂度线性增加,导致后期加载速度越来越慢.后面就需要对不同路径下的模块进行拆分,打包到相应的chunk下,按需加载,找到chunk的大小.个数和页面加载速度的 ...

随机推荐

  1. springMVC返回汉字字符串乱码,以及返回的字符串乱码的问题

    1.springMVC在使用@ResponseBody注解返回字符串为什么出现乱码呢?(这里以spring4.3.1为例) 原因分析:原因在返回字符串时StringHttpMessageConvert ...

  2. eas之执行sql的方式

    客户端:    1. 有返回集合:查询     //查询出DB中所有该字段的值,与其进行比较,若有相同的则报错      String sql="select CFWuliaoCode fr ...

  3. Centos7从3.10升级内核到4.9后无法启动解决办法:mpt[23]sas驱动问题

    Centos7升级内核后无法启动解决办法:mpt[23]sas驱动问题 前言 这个问题存在有一段时间了,之前做的centos7的ISO,在进行内核的升级以后就存在这个问题: 系统盘在板载sata口上是 ...

  4. C#第十节课

    类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thr ...

  5. 爬虫写法进阶:普通函数--->函数类--->Scrapy框架

    本文转载自以下网站: 从 Class 类到 Scrapy https://www.makcyun.top/web_scraping_withpython12.html 普通函数爬虫: https:// ...

  6. Mongodb--切片

    1.在3台服务器上分别运行 2717 , 27018,27109,互为副本集,形成3套replSet 2.在3台服务器上各配置config.server,运行在27020端口上,和配置mongod的命 ...

  7. [bzoj1452][JSOI2009]Count_树状数组

    Count bzoj-1452 JSOI-2009 题目大意:请维护一个平面内的数据结构,支持:单点修改,查询矩形内给定权值出现次数. 注释:$1\le n,m\le 300$,$1\le Q \le ...

  8. JavaScript 事件代理绑定

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. GitHub分支项目不支持搜索问题解决:Sorry, forked repositories are not currently searchable.

    错误如下: 在GitHub上的项目,如果是分支项目的星星数没有超过父项目,就不会去索引从而不会进行代码搜索. 解决方法: 1.要在搜索结果中包含分支,需要添加fork:true或fork:only查询 ...

  10. 基于DPI(深度报文解析)的应用识别2------实际分析

    新浪微博的分析 早上刚刚起床先刷微博,打算就分析一下新浪微博.登陆之后抓取公布微博的数据包.进行分析. 1.抓包的要点: 1.关闭其它网络应用,保证本机网络流量的干净,便于分析. 2.先开启wires ...