简体   繁体   English

TypeScript和systemjs圈依赖

[英]TypeScript and systemjs circle dependency

I have a problem with circle dependencies of modules: 我对模块的循环依赖有疑问:

even.ts even.ts

import { Odd } from './odd';

export class Even {

    log(){
        return console.log(Odd);        
    }  
}

odd.ts odd.ts

import { Even } from './even';

export class Odd extends Even{

  log(){
        return console.log(Even);       
    }  

}

Config: 配置:

System.config({
  defaultJSExtensions: true,
  baseURL: '/',
  transpiler: 'typescript'
});

System.import('even.js').then( a => console.log(a));

I tried to use requre.js but it can't resolve circle dependencies. 我尝试使用requre.js,但无法解决圆依赖关系。 In systemjs documentation is written that it can resolve circle depedencies but it doesn't work. 在systemjs 中编写了可以解决圆缺陷的文档,但是它不起作用。 The exception is Cannot read property 'prototype' of undefined in __extends function. 唯一的例外是Cannot read property 'prototype' of undefined __extends函数Cannot read property 'prototype' of undefined

Maybe it is better to Use CommonJS, but as I know I cant use module paths from root as I can do it in AMD and SystemJS. 也许最好使用CommonJS,但是据我所知,我无法从根目录使用模块路径,因为我可以在AMD和SystemJS中使用它。

The simplest way to resolve this is to think more in terms of modules, rather than adhering to the patterns found in C# or Java (of having one class per file). 解决此问题的最简单方法是在模块方面进行更多思考,而不是坚持使用C#或Java中的模式(每个文件只有一个类)。

If your Odd and Even classes are inextricably linked, place them both in the same module (file). 如果您的OddEven类是密不可分的,请将它们放在同一模块(文件)中。 You can't use one without the other in the set up you describe, so why force a second HTTP request. 在您描述的设置中不能没有一个使用另一个,因此为什么要强制另一个HTTP请求。

Alternatively, re-visit your inheritance hierarchy... perhaps you have chosen the wrong base class if the base class needs to know about one of its specialisations. 或者,重新访问您的继承层次结构...如果基类需要了解其专业领域之一,则可能选择了错误的基类。 Maybe you are missing the true base class that both Odd and Even should extend, or maybe you could share an interface rather than a base class. 也许您缺少OddEven都应该扩展的真实基类,或者您可以共享一个接口而不是基类。

Another possibility is that you should be using delegation rather than inheritance in this case, or maybe you should be asking for the instance from a factory. 在这种情况下,另一种可能性是您应该使用委托而不是继承,或者您应该从工厂索要实例。

While circular dependency management sounds like a good feature, you can always solve the problem without it by going back to OOD and SOLID principles. 虽然循环依赖管理听起来很不错,但是您始终可以通过回到OOD和SOLID原理来解决问题而无需解决。

If you just need to fix it then doing 如果您只需要修复它,那就去做吧

System.import('./odd') .then(() => System.import('./even')) .then(a => console.log(a));

will work. 将工作。

The issue happens using both TypeScript and Babel and seems to be due to the way that they implement class inheritance in ES5 (via their _extends and _inherits methods in the transpiled code). 使用TypeScript和Babel都会发生此问题,这似乎是由于他们在ES5中实现类继承的方式(通过转码后的_extends和_inherits方法)。 These methods rely on the module containing the superclass to have been fully executed, however SystemJS does not know this and so it is not executing the modules in the correct order. 这些方法依赖于包含超类的模块已被完全执行,但是SystemJS不知道这一点,因此它没有以正确的顺序执行模块。

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

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