【问题标题】:lazy loading of components in angular with browserify使用 browserify 以角度延迟加载组件
【发布时间】:2015-07-01 03:06:15
【问题描述】:

我正在概述一个基于 Angular 的相当复杂的应用程序的架构。所以我从 angular-seed 项目开始,这似乎是一个很好的起点。令我困扰的是,角度应用程序本质上涉及预先加载所有内容。使用脚本加载器,似乎没有一个干净的方法。

我来自backbone.js 背景,使用require.js 进行基于路由器回调的延迟加载是很安静的。角度路线的定义如下:

// Declare app level module which depends on views, and components
angular.module('myApp', [
  'ngRoute'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
.otherwise({redirectTo: '/view1'});
}]);

现在在这里,$routeProvider.when({templateURL:'../tmpl.html',controller:'view1Ctrl'}) 我想延迟加载控制器和模板。我很想使用类似的东西:

$routeProvider.when({templateURL:'../tmpl.html',controller:require('view1Ctrl'})

使用 browserify 但它似乎并不干净,甚至不需要。我知道这个问题已经以某种方式被问过好几次了,但我还没有找到明确的答案。

我的偏好是使用 browserify,因为它支持浏览器中备受喜爱的 cjs 模块。

【问题讨论】:

  • 正如你所说,由于 Angular.js 的性质,有一种“非 hacky 方式”可以做到这一点。您必须在某些时候公开$controllerProvider.register$provide.factory$compileProvider.directive,才能在运行状态下使用它们。但是,这是一个黑客。 :(
  • 另外,我认为这是相关的:stackoverflow.com/questions/21742495/…

标签: javascript angularjs browserify


【解决方案1】:

我不确定如何使用 Browserify 执行此操作,因为我自己从未尝试过,但我强烈建议您查看 ocLazyLoad

作为一个独立的服务,它在加载文件(json、css、js、模板——你可以命名)并将其注入到你已经运行的 Angular 应用程序中创造奇迹。

话虽如此,它甚至更好(imo)与路由器(默认的角度路由器,或ui-router)结合使用。

有一些“种子项目”展示了如何将 ocLazyLoad 与 SystemJS 结合使用。


但你甚至不需要那个。

如果你使用 ui-router、ui-router-extras 和 ocLazyLoad,你可以把这样的东西放在一起来延迟加载状态:

ma​​in.js

/** 
 * Inject the needed dependencies into our main module.
 */
var main = angular.module('main', [ 'ui.router', 'ct.ui.router.extras.future', 'oc.lazyLoad' ]);

/**
 * Define the lazy loaded states.
 */
main.constant('lazyLoadedStates', [
  {
    name: 'about',
    url:  '/about',
    type: 'lazy',
    src: [
      '/path/to/about.module.js',
      '/path/to/AboutController.js'
    ]
  }
]);

/**
 * Setup the behaviour for when we hit a futureState with the 'lazy'
 * type. 
 * 
 * 1. Setup a deferred object.
 * 2. Resolve the promise when every file defined in the futureState.src has been loaded.
 * 3. Return the promise.
 */
main.config(function ($futureStateProvider, lazyLoadedStates) {
  $futureStateProvider.stateFactory('lazy', function ($q, $ocLazyLoad, futureState) {
    var deferred = $q.defer();

    $ocLazyLoad.load(futureState.src).then(function () {
      deferred.resolve();
    });

    return deferred.promise;
  });

  lazyLoadedStates.forEach($futureStateProvider.futureState);
});

这就是“框架”——现在您只需要继续添加更多模块,使用更多代码,并将 real 状态定义与 dummy 匹配lazyLoadedStates 常量中的一个。

about.module.js

/** 
 * Setup the _real_ '/about' state in this lazy loaded file.
 */
angular.module('about', []).config(function ($stateProvider) {
  $stateProvider.state('about', {
    url: '/about',
    controller: 'AboutController',
    template: 'some_template.html'
  });
});

AboutController.js

/** 
 * Register the AboutController in a lazy loaded file. This could be done in about.module.js aswell,
 * but we'll do it here to separate stuff and showcase loading of multiple files. 
 */
angular.module('about').controller('AboutController', function ($state) {
  console.log('Im on a lazy loaded state!', $state.current);
});

我希望这能让您大致了解如何在 Angular 中设置延迟加载状态。我还没有找到更简单的方法来做到这一点,但我确信那里有关于如何将 ui-router(或默认的角度路由器)与其他一些“lazy-loader”耦合的文章。

文档链接:

【讨论】:

    猜你喜欢
    • 2016-09-13
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多