Created
December 9, 2016 13:11
-
-
Save creimers/73650deea2d3b4801cd3a25481d54783 to your computer and use it in GitHub Desktop.
Lazy loading with angular1 component router and webpack code split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class lazyAppCtrl { | |
constructor($q, $rootRouter, $ocLazyLoad) { | |
this.$q = $q | |
this.$rootRouter = $rootRouter | |
this.$ocLazyLoad = $ocLazyLoad | |
this.$rootRouter.config([ | |
{ | |
path: '/lazy', | |
name: 'Lazy', | |
loader: () => { | |
let deferred = this.$q.defer(); | |
require.ensure([], () => { | |
const module = require('./path/to/your/lazy/view/component') | |
return this.$ocLazyLoad.inject({name: module.default}) | |
.then(() => deferred.resolve('nameOfYourLazyViewComponent') ) | |
}); | |
return deferred.promise | |
} | |
} | |
]) | |
} | |
} | |
cont lazyApp { | |
controller: 'lazyAppCtrl', | |
template: '...' | |
} | |
angular.module('App', [require('oclazyload')]) | |
.component('lazyApp', lazyApp) | |
.controller('lazyAppCtr', lazyAppCtrl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment