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. C 语言复杂声明

    int board [8] [8] ; //声明一个内含 int 数组的数组 int ** ptr ; //声明一个指向指针的指针,被指向的指针指向 int int * risks [10] ; // ...

  2. Python 文件操作 day2

    一.打开文件的模式1:读.写.追加 1.r读:读模式,只能读,不能写,打开不存在的文件会报错:可省略'r',因为不写默认是读模式 f = open('filename',encoding='utf-8 ...

  3. python 生成HTmL报告页面

    计划做一个html页面 py3.4 代码: # -*- coding=utf-8 -*- # import time,os class Template_mixin(object): "&q ...

  4. 61.index CUD

    主要知识点 索引CUD     一.创建索引的语法     PUT /my_index { "settings": { ... any settings ... }, " ...

  5. 5、Linux的常用命令

    ls 查看当面目录结构 ls -l 列表查看当前目录 cd:切换目录 pwd:显示目前的目录 mkdir:创建一个新的目录 rmdir:删除一个空的目录 cp: 复制文件或目录 rm: 移除文件或目录 ...

  6. vue采坑一:全局API

    Vue.set Vue.set( target, key, value ),target不能是 Vue 实例,或者 Vue 实例的根数据对象,因为源码中做了如下判断: var ob = (target ...

  7. 关于autoupgader的狗屎问题

    由于win7和xp的权限问题,导致这个自动升级玩意不正常.这个狗屎问题很简单,把exe文件的兼容性设定该一下.真是气死洒家了.

  8. WordPress 在Ubuntu下安装插件、主题输入FTP及无法创建目录的问题

    1.安装新主题.插件需要输入FTP的账户密码 如果不想输入的话可以使用在wp-config.php文件中添加脚本方式. define("FS_METHOD","direc ...

  9. 洛谷 P1535 游荡的奶牛

    P1535 游荡的奶牛 题目描述 Searching for the very best grass, the cows are travelling about the pasture which ...

  10. Android Studio 导入的项目编码错误问题

    错误提示: Error:(4, 35) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 解决方法: 1). 2). 的下面添加下面的一行语句: android{compileOptions.enco ...