vue+webpack实现懒加载的三种方式

实现方式:

1、webpack method

  require.ensure([''], callback, chunkName) ;

2、es6 motehod

  import()

  import().then()

  import(/* webpackChunkName: async-chunk-name */ /* webpackMode: lazy */ modulename)

  需要安装bable插件 syntax-dynamic-import,因为import只能出现在页面顶部

 

具体实现:

第一种、

  

var component = (resolve) => {
    import(/* webpackChunkName:'vendor'*/'组件路径').then(module => {
       resolve(module)
    })      
}

第二种、

var component = (resolve) => {
    require(['组件路径'], resolve)  
}

第三种、

var component = (resolve) => {
  require.ensure([], () =>{
       resolve(reauire('组件路径'));
    }, chunkName)
}

 

posted @ 2019-10-17 21:18  欢乐之家  阅读(494)  评论(0编辑  收藏  举报