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 },
    ]
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3是一种用于构建用户界面的JavaScript框架,它具有快速、灵活、可重用等特点。Webpack则是一种JS的打包工具,可以将多个JS文件打成一个JS文件。Vue-RouterVue框架中的路由管理器,通过它可以实现前端路由的切换。Axios则是一种基于Promise的 HTTP 库,用于处理前端与后端的数据交互。Vuex则是Vue框架中的状态管理工具,将组件的共享状态抽取出来,以集中管理,便于开发人员管理。这些技术都是前端开发中必不可少的技术,用于优化前端项目的结构,提高开发效率。 在具体的项目开发中,为了提高代码的复用性,通常会对这些技术进行进一步的封装。对于Vue-Router,可以将项目的路由进行封装,创建一个Route.js文件,用于统一管理路由。通过这样的封装,可以使得路由的管理更加清晰。 对于Axios,可以通过封装一个API.js文件,将后端接口进行统一管理,减少代码的重复性。在API.js中可以封装所有后端接口的请求方法,统一处理请求返回的数据。 对于Vuex,则可以将项目的状态进行封装,写一个store.js文件,用于集中管理应用的状态。在store.js中可以设置全局数据,方便在各个组件中进行访问和修改。同时,也可以将状态的变化通过mutations.js文件封装,以保证数据的可靠性。 最后,使用Webpack进行打包。Webpack可以将多个JS文件打成一个JS文件,减少了请求的次数,提高了页面的加载速度。 综上所述,通过对Vue3、WebpackVue-Router、Axios、Vuex等技术的封装,可以提高代码的复用性和可维护性,从而加快项目的开发进度。同时,这些技术的结合还能够为项目提供更好的架构,提升用户的使用体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值