简体   繁体   English

我应该在大型角度应用程序中实例化子模块吗?

[英]Should I instantiate a submodule in large angular apps

I am building a large angular app comprised of various modules 我正在构建一个包含各种模块的大型角度应用程序

- app
  -- member
    --- newMember
    --- memberDashboard
  -- linguistics
  -- etc
     --- etc etc

In the html I am instantiating individual modules, because I believe there to be a performance improvement over bootstrapping the entire app . 在html中,我实例化了单个模块,因为我认为通过自举整个app可以提高性能。

<div ng-app="linguistics">
    <div ui-view autoscroll="true"></div>
</div>

However, this means that I have to repeat myself in module configuration. 但是,这意味着我必须重复进行模块配置。 ie: 即:

angular.module('linguistics', [
    'ui.router',
    'ui.bootstrap',
    'googlechart',
    'babelServices',
    'babelFilters'
]).config(function($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
    $locationProvider.html5Mode(true);
}).config(function($provide) {
    return $provide.decorator('$uiViewScroll', function($delegate, $window) {
        return function(uiViewElement) {
            //eventually do something more intelligent with the uiViewElement
            return $window.scrollTo(0, 0);
        };
    });
});

html5Mode, scrollTo etc will be repeated across modules. html5Mode,scrollTo等将在模块之间重复。 Is there a better way to do this? 有一个更好的方法吗?

I'd add a configuration module, that will handle the grunt work of setting up html5Mode, scrollTo etc. 我将添加一个配置模块,该模块将处理设置html5Mode,scrollTo等的繁琐工作。

angular.module('configuration', ['ui.router'])
  .config(function ($locationProvider, $provide) {
    $locationProvider.html5Mode(true);

    // For angular 1.3
    $locationProvider.html5Mode({
      enabled: true,
      requireBase: /* true/false */
    });

    $provide.decorator('$uiViewScroll', function ($delegate, $window) {
      return function (uiViewElement) {
        return $window.scrollTo(0, 0);
      }
    });
  });

And then in your subsequent modules: 然后在您的后续模块中:

angular.module('linguistics', ['configuration']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM