简体   繁体   English

AngularJS:最佳实践

[英]AngularJS: Best practices

I don't think this question is considered opinion-based since it is asking about best practices, but at the same time I don't think there are any industry standards for AngularJS best practices, so it's hard to avoid opinions in some cases. 我不认为这个问题被认为是基于意见的,因为它询问最佳实践,但同时我认为AngularJS最佳实践没有任何行业标准,因此在某些情况下很难避免意见 In other cases, one way of programming is clearly better than the other. 在其他情况下,一种编程方式明显优于另一种方式。 I am not sure which category this question falls under. 我不确定这个问题属于哪个类别。

I don't think there are any official AngularJS best practices. 我认为AngularJS没有任何官方最佳实践。 There are guidelines, and some people have released their own best practices, but I haven't stumbled upon an industry standard. 有指导方针,有些人已经发布了自己的最佳实践,但我没有偶然发现行业标准。

I have a question about how I'm organizing my modules and the code itself. 我有一个关于我如何组织模块和代码本身的问题。 It feels like I have a lot of small files and too many folders, even though I have broken down the app into common components and core features. 感觉就像我有很多小文件和太多文件夹,即使我已经将应用程序分解为常见组件和核心功能。 It's still a relatively small application, but it's likely to grow. 它仍然是一个相对较小的应用程序,但它可能会增长。

The biggest question I have was how I should organize my submodules. 我遇到的最大问题是我应该如何组织我的子模块。 Currently each submodule is broken down into various files, separating controllers, directives, and services. 目前,每个子模块被分解为各种文件,分离控制器,指令和服务。 I was wondering if I should just define them all in one file, and then if the submodule gets too large, break it down again. 我想知道我是否应该在一个文件中定义它们,然后如果子模块太大,再次将其分解。

For instance, right now I have a module app.buttonToggle , with various files: 例如,现在我有一个模块app.buttonToggle ,包含各种文件:

buttonToggle.app.js buttonToggle.app.js

angular.module('app.buttonToggle', []);

buttonToggleCtrl.js buttonToggleCtrl.js

angular.module('app.buttonToggle').controller('ButtonToggleCtrl', ButtonToggleCtrl);

function ButtonToggleCtrl() ...
...

buttonToggle.js buttonToggle.js

angular.module('app.buttonToggle').directive('buttonToggle', buttonToggle);

function buttonToggle() ...
...

Instead would it be better to combine them into one file? 相反,将它们组合成一个文件会更好吗?

buttonToggle.app.js buttonToggle.app.js

angular.module('app.buttonToggle', [])

    .controller('ButtonToggleCtrl', function() { ... })

    .directive('buttonToggle', function() { ... });

What would be the benefit of doing the latter, if any? 如果有的话,做后者的好处是什么? I like how it uses less code, but is it harder to test? 我喜欢它如何使用更少的代码,但是它更难测试?

I recommend using John Papa's style guide - it is what I use to help make these decisions and keep up with and potentially contribute to the best practices. 我推荐使用John Papa的风格指南 - 这是我用来帮助做出这些决定并跟上并可能为最佳实践做出贡献的方法。 It will help you with this issue. 它将帮助您解决此问题。 These style guides are becoming very popular to help you with your questions. 这些风格指南正变得非常受欢迎,可以帮助您解决问题。

In this case, the community agrees you should have one component per file and use a task runner like gulp or grunt to package for shipping. 在这种情况下,社区同意每个文件应该有一个组件,并使用像gulp或grunt这样的任务运行器来打包运输。

https://github.com/johnpapa/angular-styleguide https://github.com/johnpapa/angular-styleguide

The main benefit here 这里的主要好处

angular.module('app.buttonToggle').controller('ButtonToggleCtrl', ButtonToggleCtrl);

is that (due to JS hoisting) controller constructor can annotated in readable manner, eg 是(由于JS提升)控制器构造函数可以以可读的方式注释,例如

ButtonToggleCtrl.$inject = ['...'];
function ButtonToggleCtrl(...) {

It isn't an issue when automatic annotation is used. 使用自动注释时不会出现问题。

It is also a bit more convenient to debug named functions rather than anonymous ones, though Angular provides meaningful feedback on errors in all cases. 调试命名函数而不是匿名函数也更方便,尽管Angular在所有情况下都提供有意义的错误反馈。

What would be the benefit of doing the latter, if any? 如果有的话,做后者的好处是什么? I like how it uses less code, but is it harder to test? 我喜欢它如何使用更少的代码,但是它更难测试?

It isn't. 事实并非如此。 All of module components can be tested separately irregardless of how they were defined, as long as their definitions don't contain syntactic errors. 只要它们的定义不包含语法错误,所有模块组件都可以单独测试,无论它们是如何定义的。

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

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