当我可以在es6中使用导入/导出时,角依赖注入的优点是什么

时间:2021-11-05 14:50:20

I just moved into a company using AngularJS (1.x) stack. Coming from a es6(babel)+webpack+redux+react background, I was wondering what are the benefits of using AngularJS dependency injection when I can simply import/export files. Frankly, import/export gives me locations of files to look for, but AngularJS dependency injection doesn't seem to have that in mind. (Also, angularJS services, factories, etc. seem to be replaceable with ES6 classes, export/imports, etc.)

我刚进入一家使用AngularJS (1.x)栈的公司。来自es6(babel)+webpack+redux+的反应背景,我想知道使用AngularJS依赖注入的好处是什么,我可以简单地导入/导出文件。坦率地说,导入/导出提供了要查找的文件的位置,但是AngularJS依赖注入似乎并没有考虑到这一点。(同时,angularJS服务、工厂等似乎可以替代ES6类、导出/导入等)

Are there any noticeable benefits on using AngularJS dependency injection and angularjs-specific dependencies (such as $http, etc.) rather than moving to alternatives? (such as jquery's ajax, etc.)

使用AngularJS依赖注入和特定于AngularJS的依赖项(比如$http等),而不是转向其他替代方法,有什么明显的好处吗?(例如jquery的ajax等)

thank you : )

谢谢你:)

1 个解决方案

#1


3  

Module loading and Dependency Injection are two different ideas.

模块加载和依赖注入是两个不同的想法。

Module loading is about locating your source. DI is about automatically wiring your objects together.

模块加载是关于定位源的。DI是关于自动将对象连接到一起的。

Angular allows you to specify a provider which gives you the ability to configure your code before injection. I recommend you take a look at this link.

角允许您指定提供程序,使您能够在注入之前配置代码。我建议你看看这个链接。

https://docs.angularjs.org/guide/providers

https://docs.angularjs.org/guide/providers

The other value that angular's DI framework has is making it easier to write tests as you can easily replace your services.

角的DI框架的另一个价值是使编写测试变得更容易,因为您可以轻松地替换您的服务。

As for moving away from Angular specific services, I would recommend against that as these services are not hooked into your scope, so you may have to call $scope.apply() everywhere.

至于远离特定于角的服务,我建议不要这样做,因为这些服务没有连接到您的范围中,所以您可能需要在任何地方调用$scope.apply()。

All that said, you could argue that DI is less useful in a language like Javascript as it is not class driven such as Java and C#.

尽管如此,您可能会认为DI在像Javascript这样的语言中并不是很有用,因为它不是像Java和c#这样的类驱动的。

#1


3  

Module loading and Dependency Injection are two different ideas.

模块加载和依赖注入是两个不同的想法。

Module loading is about locating your source. DI is about automatically wiring your objects together.

模块加载是关于定位源的。DI是关于自动将对象连接到一起的。

Angular allows you to specify a provider which gives you the ability to configure your code before injection. I recommend you take a look at this link.

角允许您指定提供程序,使您能够在注入之前配置代码。我建议你看看这个链接。

https://docs.angularjs.org/guide/providers

https://docs.angularjs.org/guide/providers

The other value that angular's DI framework has is making it easier to write tests as you can easily replace your services.

角的DI框架的另一个价值是使编写测试变得更容易,因为您可以轻松地替换您的服务。

As for moving away from Angular specific services, I would recommend against that as these services are not hooked into your scope, so you may have to call $scope.apply() everywhere.

至于远离特定于角的服务,我建议不要这样做,因为这些服务没有连接到您的范围中,所以您可能需要在任何地方调用$scope.apply()。

All that said, you could argue that DI is less useful in a language like Javascript as it is not class driven such as Java and C#.

尽管如此,您可能会认为DI在像Javascript这样的语言中并不是很有用,因为它不是像Java和c#这样的类驱动的。