简体   繁体   English

使用es6模块的角度服务是否有意义?

[英]Does it make sense to use angular services with es6 modules?

Does it make sense to use angular-services when we use ES6 modules? 使用ES6模块时使用角度服务是否有意义? For example we need a singleton module ( userService ) in our code and we can do like this: 例如,我们的代码中需要一个单独的模块( userService ),我们可以这样做:

var app = angular.module('app', []);

app.service('userService', function(){
    this.users = ['John', 'James', 'Jake'];
});

app.controller('FooController', ['$scope', 'userService', function($scope, userService){

    console.log(userService);

}]);

But we can define the service in separate file: 但我们可以在单独的文件中定义服务:

/* ./user-service.js */
export default users = ['John', 'James', 'Jake'];

, then make the code like this: ,然后像这样编写代码:

var app = angular.module('app', []);

var userService = require('./user-service')    

app.controller('FooController', ['$scope', function($scope){

    console.log(userService);

}]);

and result will be absolutely the same as with services using. 和结果将与使用的服务完全相同。 So why use angular services when we can use modules? 那么为什么在我们可以使用模块时使用角度服务?

Yes! 是! It makes perfect sense. 这很有道理。

Services implement a particular responsibility in your application, moving data between the data store and views. 服务在您的应用程序中实现特定的责任,在数据存储和视图之间移动数据。

Modules allow you to organize your code and separate sections with different responsibilities. 模块允许您组织代码并分离具有不同职责的部分。

By putting each service into a module, you make it easier to browse and test your code. 通过将每个服务放入模块,您可以更轻松地浏览和测试代码。 It's easy to find all of the code that implements a responsibility. 很容易找到实现责任的所有代码。

Source: Difference between service, directive and module =) 来源: 服务,指令和模块之间的差异 =)

From my own personal notes (mostly snippets from the docs, google group posts, and SO posts): 从我自己的个人笔记 (主要是来自文档的片段,谷歌小组帖子和SO帖子):

Modules 模块

  • provide a way to namespace/group services, directives, filters, configuration information and initialization code 提供命名空间/组服务,指令,过滤器,配置信息和初始化代码的方法
  • help avoid global variables 帮助避免全局变量
  • are used to configure the $injector, allowing the things defined by the module (or the whole module itself) to be injected elsewhere (Dependency Injection stuff) 用于配置$ injector,允许模块(或整个模块本身)定义的东西注入其他地方(依赖注入的东西)
  • Angular modules are not related to CommonJS or Require.js. Angular模块与CommonJS或Require.js无关。 As opposed to AMD or Require.js modules, Angular modules don't try to solve the problem of script load ordering or lazy script fetching. 与AMD或Require.js模块相反,Angular模块不会尝试解决脚本加载排序或延迟脚本提取的问题。 These goals are orthogonal and both module systems can live side by side and fulfill their goals (so the docs claim). 这些目标是正交的,两个模块系统可以并存并实现其目标(因此文档声称)。

Services 服务

  • are singletons, so there is only one instance of each service you define. 是单身人士,因此您定义的每项服务只有一个实例。 As singletons, they are not affected by scopes, and hence can be accessed by (shared with) multiple views/controllers/directives/other services 作为单身人士,他们不受范围的影响,因此可以通过(共享)多个视图/控制器/指令/其他服务来访问
  • You can (and probably should) create custom services when 您可以(也可能应该)在何时创建自定义服务
    • two or more things need access to the same data (don't use root scope) or you just want to neatly encapsulate your data 两个或多个东西需要访问相同的数据(不要使用根作用域)或者你只是想整齐地封装你的数据
    • you want to encapsulate interactions with, say, a web server (extend $resource or $http in your service) 你想封装与web服务器的交互(在你的服务中扩展$ resource或$ http)
  • Built-in services start with '$'. 内置服务以'$'开头。
  • To use a service, dependency injection is required on the dependent (eg, on the controller, or another service, or a directive). 要使用服务,需要依赖注入(例如,在控制器上,或其他服务或指令)。

Directives (some of the items below say essentially the same thing, but I've found that sometimes a slightly different wording helps a lot) 指令 (下面的一些项目基本上是相同的,但我发现有时候略有不同的措辞有很多帮助)

  • are responsible for updating the DOM when the state of the model changes 负责在模型状态发生变化时更新DOM
  • extend HTML vocabulary = teach HTML new tricks. 扩展HTML词汇=教HTML新技巧。
    Angular comes with a built in set of directives (eg, ng-* stuff) which are useful for building web applications but you can add your own such that HTML can be turned into a declarative Domain Specific Language (DSL). Angular附带了一组内置指令(例如,ng- * stuff),这些指令对于构建Web应用程序很有用,但您可以添加自己的指令,以便将HTML转换为声明性域特定语言(DSL)。 Eg, the <tabs> and <pane> elements on the Angular home page demo "Creating Components". 例如,Angular主页上的<tabs>和<pane>元素演示“创建组件”。
    • Non-obvious built-in directives (because they don't start with "ng"): a, form, input, script, select, textarea. 非显而易见的内置指令(因为它们不以“ng”开头):a,form,input,script,select,textarea。 Under Angular, these all do more than normal! 在Angular下,这些都比正常更多!
  • Directives allow you to "componentize HTML". 指令允许您“组件化HTML”。 Directives are often better than ng-include. 指令通常比ng-include更好。 Eg, when you start writing lots of HTML with mainly data-binding, refactor that HTML into (reusable) directives. 例如,当您开始编写大量主要是数据绑定的HTML时,将HTML重构为(可重用)指令。
  • The Angular compiler allows you to attach behavior to any HTML element or attribute and even create new HTML elements or attributes with custom behavior. Angular编译器允许您将行为附加到任何HTML元素或属性,甚至可以使用自定义行为创建新的HTML元素或属性。 Angular calls these behavior extensions directives . Angular调用这些行为扩展指令
    • When you boil it all down, a directive is just a function which executes when the Angular compiler encounters it in the DOM. 当你把它全部烧掉时,指令只是一个在Angular编译器在DOM中遇到它时执行的函数。
  • A directive is a behavior or DOM transformation which is triggered by a presence of an attribute, an element name, a class name, or a name in a comment. 指令是一种行为或DOM转换,它由注释中存在的属性,元素名称,类名或名称触发。 Directive is a behavior which should be triggered when specific HTML constructs are encountered in the (HTML) compilation process. 指令是在(HTML)编译过程中遇到特定HTML构造时应触发的行为。 The directives can be placed in element names, attributes, class names, as well as comments. 指令可以放在元素名称,属性,类名以及注释中。
    • Most directives are restricted to attribute only. 大多数指令仅限于属性。 Eg, DoubleClick only uses custom attribute directives. 例如,DoubleClick仅使用自定义属性指令。
  • see also What is an angularjs directive? 另请参见什么是angularjs指令?

Define and group Angular things (dependency injection stuff) in modules. 在模块中定义和分组Angular事物(依赖注入事物)。
Share data and wrap web server interaction in services. 共享数据并在服务中包装Web服务器交互。
Extend HTML and do DOM manipulation in directives. 扩展HTML并在指令中执行DOM操作。
And make Controllers as "thin" as possible. 并使控制器尽可能“薄”。

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

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