zoukankan      html  css  js  c++  java
  • (转)Webpack2 + Vue2 + Vue-Router2 如何实现懒加载?

    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 },
        ]
    })
  • 相关阅读:
    大部分人都会做错的经典JS闭包面试题
    20071109 QQ群:ITIL和ITSM 聊天记录,欢迎大家加入QQ群:48132184
    模式窗口window.open造成Session丢失的解决方法
    Server.Transfer 方法如何传递复杂的参数
    小游戏测试你的情商
    VS.Net 开发 MSN一样缓慢出来的提示信息的方法
    Javascript 技巧大全
    ASP.NET 2.0,无刷新页面新境界! 【转】
    ASP.NET AJAX入门系列【转】
    asp.net2.0+ajax开发的无刷新聊天室Demo【转】
  • 原文地址:https://www.cnblogs.com/lsy0403/p/8665081.html
Copyright © 2011-2022 走看看