gpt4 book ai didi

javascript - 继承和依赖注入(inject)如何与 Angular 模块一起工作?

转载 作者:行者123 更新时间:2023-11-29 14:50:58 24 4
gpt4 key购买 nike

我对 Angular 模块的工作方式以及使用它们的最佳方式感到有点困惑。我在一本书中读到,无论哪个模块包含服务,服务都是全局可用的。过滤器和指令显然也可以在整个应用程序的模板文件中使用。

一个简单的例子:

angular.module('main',[
'one',
'two',
'three'
]);


angular.module('one',[]).service('oneService', ...);
angular.module('two',[
// angular-module 'one' does not have to be passed here
]).controller(function(oneService){
// available here
oneService.do();

});

angular.module('three',[]);


// not a good idea
angular.module('three').service('oneService', ...);

这实际上给我带来了几个问题:

  • 如果没有真正的闭包,“思考” Angular 模块的好方法是什么?
  • 除了语义和由此产生的文件夹结构(可以在不使用模块的情况下完成)之外 - 将一个应用程序拆分为不同模块的好处是什么?
  • 在模块中使用点符号时是否会发生任何继承自动魔法,例如angular.module('main')angular.mopdule('main.customers')?
  • 将一个应用程序拆分成不同的应用程序(而不是模块)是否是一种可能的替代方案,例如每条路线一个应用程序,如果是这样,什么是解决它的好方法,as apps cannot contain other apps

最佳答案

Miško Hevery 在这里讨论:

https://www.youtube.com/watch?v=ZhfUv0spHCY&t=34m19s

然后是 Naomi Black 的帖子:

http://blog.angularjs.org/2014/02/an-angularjs-style-guide-and-best.html

其中引用了这些文档:

https://google-styleguide.googlecode.com/svn/trunk/angularjs-google-style.html https://docs.google.com/document/d/1XXMvReO8-Awi1EZXAXS4PzDzdNvV6pGcuaF4Q9821Es/pub

还有最新版的angular文档states :

... we recommend that you break your application to multiple modules like this:

  • A module for each feature
  • A module for each reusable component (especially directives and filters)
  • And an application level module which depends on the above modules and contains any initialization code.

总结:

  • 模块只是配置 DI 子系统的一种方式(至少到目前为止)
  • 如果您确实想使用模块按功能而不是按类型拆分您的应用程序
  • 现在实际上建议使用每个功能一个模块来构建您的应用程序
  • 模块现在并没有提供太多好处,但在不久的将来,当实现 View 的延迟加载时,它们可能会提供很多好处

关于javascript - 继承和依赖注入(inject)如何与 Angular 模块一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25825450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com