简体   繁体   English

AngularJS最佳实践:组件应该将自己添加到AngularJS模块中

[英]AngularJS Best Practice: Should components add themselves to AngularJS module

I have an AngularJS project with a shared directory, in which there are several shared components. 我有一个带有shared目录的AngularJS项目,其中有几个共享组件。

- shared
    - index.js
    - alert
        - index.js
        - alert.component.js
        - alert.controller.js
        - alert.tpl.html
        - alert.scss
    - logging
        - index.js
        - logging.component.js
        - logging.controller.js
        - logging.tpl.html
        - logging.scss

The code is written in a modular way in ES6. 该代码在ES6中以模块化方式编写。 So for instance alert.component.js might look like this: 因此,例如alert.component.js可能看起来像这样:

import controller from './alert.controller'
export const Alert = {
    controller,
    templateUrl: 'shared/alert/alert.tpl.html'
};

I would like to have one AngularJS module called shared in which both components are defined. 我想要一个shared AngularJS模块,其中定义了两个组件。 My question is on what level should I actually define the component. 我的问题是我应该在什么级别上实际定义该组件。 Should it be inside the component directory ( shared/alert/index.js ) or in the shared directory ( shared/index.js ). 它应位于组件目录( shared/alert/index.js )内还是共享目录( shared/index.js )中。

In the first case, the file shared/alert/index.js , would look like this: 在第一种情况下,文件shared/alert/index.js如下所示:

import { Alert } from './alert.component';
angular.module('shared').component('Alert', Alert);

And in the second case, the file would look like this: 在第二种情况下,文件如下所示:

export { Alert } from './alert.component';

And then both components would be defined in shared/index.js : 然后两个组件都将在shared/index.js定义:

import { Alert } from './alert';
angular.module('shared').component('Alert', Alert);

import { Logging } from './logging';
angular.module('shared').component('Logging', Logging);

The first case seems a bit odd to me, since I kind of let the component add itself to the application. 第一种情况对我来说似乎有些奇怪,因为我有点让组件将自身添加到应用程序中。 In the second case however I end up with a huge index.js file, if I have many shared components. 但是,在第二种情况下,如果我有很多共享组件,则最终会得到一个巨大的index.js文件。 So I was wondering what other up and down sides each of these approaches has and if there any best practices? 因此,我想知道这些方法各自具有哪些其他方面以及是否存在最佳实践?

As with any 'best practice', the preferable way to do this is deduced from possible problems that can appear in this situation. 与任何“最佳实践”一样,从这种情况下可能出现的问题中推断出执行此操作的最佳方法。

angular.module('shared') module getter is generally an antipattern, especially in modular environment. angular.module('shared')模块的getter通常是一种反模式,尤其是在模块化环境中。 It should be evaluated after the module was defined with angular.module('shared', []) module setter. 在使用angular.module('shared', [])模块设置器定义了模块之后,应该对其进行评估。 Doing the opposite will result in race condition and error. 否则会导致竞争状况和错误。

In the first case importing shared/alert/index.js in tests or another module will result in error because shared module wasn't defined yet. 在第一种情况下,在测试或其他模块中导入shared/alert/index.js将导致错误,因为尚未定义shared模块。

In the second case there will be no such problem, but only if shared module is defined in shared/index.js . 在第二种情况下会出现没有这样的问题,但只有当shared模块中定义shared/index.js

The approach that plays well with ES modules is one module per file . 与ES模块配合使用的方法是每个文件一个模块 The result is highly modular application with relatively low amount of boilerplate code. 结果是高度模块化的应用程序,并且具有相对较少的样板代码。 The benefits are reusability and testability, submodules can depend on each other but aren't coupled to share . 好处是可重用性和可测试性,子模块可以相互依赖,但不share

This is important consideration if some of submodules contain items that affect the entire application - decorators, config/run blocks, third-party modules that contain them (router). 如果某些子模块包含影响整个应用程序的项目,则这是重要的考虑因素-装饰器,配置/运行块,包含它们的第三方模块(路由器)。

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

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