简体   繁体   English

Angular 2的最佳实践

[英]Best Practice of Angular 2

Angular 2 is considered as a complete framework in frontend development. Angular 2被认为是前端开发中的完整框架。 One of advantages is to put frontend interactions in a managed way, in contrast to various functions of jQuery. 与jQuery的各种功能相比,其中一个优点是以托管方式放置前端交互。

On the other hand, jQuery is very flexible and its features can be applied on demand function by function. 另一方面,jQuery非常灵活,其功能可以按功能按需应用。 It is well collaborated with plain HTML codes, especially good-look templates have been prepared by visual designers. 它与纯HTML代码很好地协作,特别是视觉设计师准备好的模板。 From this prospective, jQuery seems more agile. 从这个角度来看,jQuery似乎更敏捷。

What is the best practice of applying Angular 2? 应用Angular 2的最佳做法是什么? Should it be mixed with jQuery, bootstrap, and other frontend library/framework together? 它应该与jQuery,bootstrap和其他前端库/框架混合在一起吗? What responsibility should each of them take? 他们每个人应该承担什么责任?

As this question is about software design, you may read answers with various point of views, I will not pretend to detain 'the best answer' and will try to stick with facts. 由于这个问题是关于软件设计的,你可以阅读各种观点的答案,我不会假装扣留“最佳答案”,并会试着坚持事实。

jQuery was originally designed as a DOM manipulation library, plus styling via jQuery-ui plugin, plus data/asynchronous management such as Promises, Deferred, ajax calls. jQuery最初设计为DOM操作库,加上jQuery-ui插件的样式,以及Promises,Deferred,ajax调用等数据/异步管理。 These different sets of features do not interact much with each-other, besides the fact that they can be chained easily. 除了可以轻松链接的事实之外,这些不同的功能集彼此之间不会相互影响很多。 jQuery is not a framework but a library. jQuery不是一个框架,而是一个库。

  • Angular2's documentation does not recommend manipulation of DOM elements at all. Angular2的文档根本不建议操纵DOM元素。 When needed guideline is to use it's Angular2's renderer. 需要时,指南是使用它的Angular2的渲染器。
  • Angular2 offers ajax calls using it's 'http' class. Angular2使用它的'http'类提供ajax调用。
  • Angular2 plays well will rxJs Observables, Promises, etc. allowing you to be a master of asynchronous behaviour. Angular2可以很好地运行rxJs Observables,Promises等,让你成为异步行为的主人。 Downside is that the learning curve is more than steep. 缺点是学习曲线不仅仅是陡峭。
  • CSS3 is now here and supports animations really well. CSS3现在在这里并且非常支持动画。 Class binding can be done to change appearance of components. 可以进行类绑定以更改组件的外观。
  • Boostrap can be used in an Angular2 project, and doesn't need jQuery to run , unless using it's javascript library . Boostrap可以在Angular2项目中使用, 并且不需要运行jQuery ,除非使用它的javascript库

Even though, using jQuery in a project is compelling: 即使在项目中使用jQuery也很有吸引力:

  • jQuery has a great past, learning it is quite easy and has alot of users. jQuery有一个伟大的过去,学习它很容易,并有很多用户。
  • Seriously, it's performance is really good. 说真的,它的表现非常好。 Library only takes 84kb minified. 图书馆只需要84kb缩小。
  • jQuery has a huge number of plugins, plugins not having their own successor in angular2 world yet. jQuery有大量的插件,插件在angular2世界中没有自己的继承者。 For example Boostrap 'javascript' plugins are mostly (if not all) jQuery plugins. 例如,Boostrap的“javascript”插件大多是(如果不是全部)jQuery插件。 see here 看这里

One of Angulars core ideas is not to manipulate the DOM directly, which is the core idea of jQuery to do. Angulars核心思想之一不是直接操纵DOM,这是jQuery的核心思想。 So as @GünterZöchbauer says, don't use it unless you really have to (which is very rare). 因此,@GünterZöchbauer说,除非你真的需要,否则不要使用它(这是非常罕见的)。 jQuery is more like a helper library, Angular is a Framework and should be used as such. jQuery更像是一个帮助库,Angular是一个框架,应该这样使用。
You can of course mix it with Bootstrap, whereas you should only use the styles and use a Angular 2 version of Bootstrap though: https://valor-software.com/ng2-bootstrap/#/ or https://github.com/ng-bootstrap/ng-bootstrap . 您当然可以将它与Bootstrap混合使用,而您应该只使用样式并使用Angular 2版本的Bootstrap: https//valor-software.com/ng2-bootstrap/#/https://github.com / ng-bootstrap / ng-bootstrap

As for 至于

and other frontend library/framework together? 和其他前端库/框架在一起?

You shouldn't mix it with another Framework, that just messes things up. 你不应该把它与另一个框架混合在一起,这只会让事情变得混乱。 Libraries sure though, for most of the popular libraries out there exists (or will exist) a Angular version which comes with TypeScript support etc. 但是,对于大多数流行的库来说,库肯定存在(或将存在)带有TypeScript支持等的Angular版本。

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

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