Lazy Loading of Angular Component Scripts when Changing States

前端 未结 1 950
礼貌的吻别
礼貌的吻别 2021-02-01 11:01

This problem has been taking up the last day or so.

I\'ve been trying to get my AngularJS application to load the script files for each state\'s components lazily. I\'m

1条回答
  •  温柔的废话
    2021-02-01 11:30

    Here's the code for an Angular module lazy, depending on the ui.router module. When it's included in your module's dependencies, the lazy loading functionality of the state's scripts will be enabled. I've included examples of the primary app module, a few lazy components, and my index.html, sanitized for demonstration purposes. I'm using the Script.js library to actually handle the script loading.

    angular-ui-router-lazy.js

    /**
     * Defines an AngularJS module 'lazy' which depends on and extends the ui-router
     * module to lazy-load scripts specified in the 'scripts' attribute of a state
     * definition object.  This is accomplished by registering a $stateChangeStart
     * event listener with the $rootScope, interrupting the associated state change
     * to invoke the included $scriptService which returns a promise that restarts the
     * previous state transition upon resolution.  The promise resolves when the
     * extended Script.js script loader finishes loading and inserting a new 
        
      
    
      
        

    Function Hacked into Script.js because I Prefer the Syntax

    $script.queue = function(aQueueBehind,aUrl,aLabel) {
      if (aQueueBehind === null) { return $script((aUrl === null?[null]:aUrl),aLabel); }
      $script.ready(aQueueBehind,function() {
        if (aUrl !== null)
          $script(aUrl,aLabel);
        else
          $script.done(aLabel);
      });
      return $script;
    }
    

    lazyapp.module.js

    (function() {
      var lazyApp = angular && angular.module('lazyApp ',['lazy']);
      lazyApp = angular.module('lazy').makeLazy(lazyApp);
    
      lazyApp.config(function($stateProvider) {
    
        $stateProvider.state({
          name: 'root',
          url: '',
          views: {
            'mainView': { templateUrl: '/lazyapp/views/mainview.html', controller: 'lazyAppController' }
          },
          scripts: {
            'directives': [ 'lazyapp/directives/lazyheader/src/lazyheader.js' ],
            'controllers': [ 'lazyapp/controllers/lazyappcontroller.js' ],
            'services': [ 'lazyapp/services/sectionservice.js' ]
          },
          resolve: {
            sections: function(sectionService) {
              return sectionService.getSections();
            }
          }
        });
      });
    
      angular.bootstrap(document,['lazyApp']);
    })();
    

    sectionservice.js

    (function() {
      var lazyApp = angular.module('lazyApp');
    
      lazyApp.service('sectionService',function($q) {
        this.getSections = function() {
          var deferred = $q.defer();
          deferred.resolve({
            'home': {},
            'news': {},
            'events': {},
            'involved': {},
            'contacts': {},
            'links': {}
          });
          return deferred.promise;
        };
      });
    })();
    

    lazyheader.js

    (function() {
      var lazyApp = angular.module('lazyApp ');
    
      lazyApp.directive('lazyHeader',function() {
        return {
          templateUrl: 'lazyapp/directives/lazyheader/templates/lazyheader-main.html',
          restrict: 'E'
        };
      });
    })();
    

    lazyappcontroller.js

    (function() {
      var lazyApp = angular.module('lazyApp ');
    
      lazyApp.controller('lazyAppController',function(sections) {
        // @TODO: Control things.
        console.log(sections);
      });
    })();
    

    0 讨论(0)
提交回复
热议问题