简体   繁体   English

使用打字稿对角度模块的循环依赖

[英]Circular dependencies on angular modules using typescript

I'm facing this issue where i have 2 angular 1.5 modules that depend on each other, which is fine by angularjs, but when i import them using typescript & webpack, i get circular typescript-module dependencies and the modules are not loaded properly. 我遇到的这个问题是我有2个相互依赖的angular 1.5模块,angularjs很好,但是当我使用typescript和webpack导入它们时,我得到了循环的typescript-module依赖性,并且模块未正确加载。 I dont have circular dependencies in the angular services between the modules, therefor there should be no problem depending the angular-modules on each other. 我在模块之间的角度服务中没有循环依赖性,因此将角度模块彼此依赖应该没有问题。

//first.module.ts
import {SecondModule} from "second.module.ts";
let FirstModule = angular.module("firstModule",[SecondModule.name]);
export {FirstModule};

//second.module.ts
import {FirstModule} from "first.module.ts";
let SecondModule = angular.module("secondModule",[FirstModule.name]);
export {SecondModule};

In the example above, i get "cannot get 'name' of undefined" on the last line, since FirstModule is not exported yet. 在上面的示例中,由于FirstModule尚未导出,因此我在最后一行得到“无法获取未定义的'名称'”。

One solution i saw online is just to not define the SecondModule as a dependency of FirstModule, but make them both dependencies of a parent module (my main app module), the problem is that i cant mock FirstModule using ngMock for unit testing - since it does not register SecondModule as a dependent sub-module. 我在网上看到的一个解决方案是不将SecondModule定义为FirstModule的依赖项,而是使它们都成为父模块(我的主应用程序模块)的依赖项,问题是我无法使用ngMock来模拟FirstModule进行单元测试-因为它没有将SecondModule注册为从属子模块。 so i have to mock my whole main app which is big and messy. 所以我必须嘲笑我的整个主应用程序,它又大又混乱。

Other solution i saw was to wrap the angular-modules with typescript classes, and put the angular module creation in the constructor of this class, so i can control when the module is created, but then i have to refactor my whole application to create objects of those wrapped modules, and i have no idea how will this affect writing unit tests using ngMock. 我看到的另一种解决方案是将angular-modules与typescript类包装在一起,然后将angular模块的创建放入此类的构造函数中,这样我就可以控制何时创建模块,但是随后我必须重构整个应用程序以创建对象这些包装好的模块,我不知道这将如何影响使用ngMock编写单元测试。

Are there any best practices for this situation? 是否有针对这种情况的最佳做法?

Thanks! 谢谢!

You can export a string constant as the first thing in the file 您可以将字符串常量导出为文件中的第一件事

//first.module.ts
export const prefixFirstModule = "prefixFirstModule";
import {prefixSecondModule} from "second.module";
export const FirstModule = angular.module(prefixFirstModule,[prefixSecondModule]);

//second.module.ts
export const prefixSecondModule = "prefixSecondModule";
import {prefixFirstModule} from "first.module";
export const SecondModule = angular.module(prefixSecondModule,[prefixFirstModule]);

It's a bit of a pain, but as far as I'm aware it's the only way round the problem with the same support for tree shaking etc with your current module structure. 这有点痛苦,但是据我所知,这是解决当前模块结构对树抖动等问题的相同支持的唯一方法。

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

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