与角2和SystemJS循环依赖 [英] Circular dependency with Angular 2 and SystemJS
问题描述
我有,我认为是一个循环依赖造成了问题。一些广泛的研究后,我一直没能找到解决的办法。它看起来与此相关的问题:类型错误:B是在__extends未定义的打字稿的,但它没有帮助我。
我已经能够简化这个plunker 。
基本上有3类:
- 类
A
,包含数组A
- 类
B
,从继承A
- 类
˚F
,一个工厂,可以创建一个A
或B
取决于值
这样做的目的是处理可以是动态的(每个 A
是一个参数,可以是一个数组)的参数列表,其中乙
是 A
来处理文件的一个特例。我试图消除了工厂,但只有 A
和 B
我仍然得到了同样的错误:
类型错误:b为未定义
错误加载的http://本地主机:3000 /应用/ main.js
下面是在code a.ts
进口{F}从'./f';出口类A {
儿:[] 构造函数(hasChildren:布尔= FALSE){
如果(hasChildren){
为(变量I = 0; I&小于10 ++ⅰ){
让ISB =(的Math.random()* 2)> 1;
this.children.push(F.createObject(ISB))
}
}
}
}
b.ts
从./a'进口{A};出口类B扩展A {
}
和 f.ts
进口{A}从'./a
进口{B}从'./b出口F级{
静态的CreateObject(ISB:布尔):A {
如果(ISB){
返回新型B
}其他{
返回新的A
}
}
}
您不能有一个循环依赖这种方式。您可以通过使用一个接口解决
itoto.js
进口{} IToto从'./itoto';
出口类塔塔实现IToto {
孩子:托托[]
}
toto.js
进口{}塔塔从'./tata';
进口{} IToto从'./itoto';出口类托托实现IToto {
孩子:托托[] = []; 构造函数(hasChildren:布尔= FALSE){
...
}
}
tata.ts
进口{} IToto从'./itoto';
出口类塔塔实现IToto {
孩子:托托[]
}
I have a problem that I think is caused by a circular dependency. After some extensive research, I haven't been able to find a solution. It looks related to this issue : TypeError: b is undefined in __extends in TypeScript, but it didn't help me.
I have been able to simplify the problem in this plunker.
Basically there are 3 classes :
- the class
A
, that contains an array ofA
- the class
B
, that inherits fromA
- the class
F
, a factory that can create anA
or aB
depending on a value
The purpose of this is to handle a parameter list that can be dynamic (each A
is a parameter and can be an array) and where B
is a specialization of A
to handle files. I tried removing the factory, but with only A
and B
I still get the same error :
TypeError: b is undefined
Error loading http://localhost:3000/app/main.js
Here is the code of a.ts
import { F } from './f';
export class A {
children: A[]
constructor(hasChildren: boolean = false) {
if (hasChildren) {
for (var i = 0 ; i < 10 ; ++i) {
let isB = (Math.random() * 2) > 1;
this.children.push(F.createObject(isB))
}
}
}
}
b.ts
import { A } from './a';
export class B extends A {
}
and f.ts
import { A } from './a'
import { B } from './b'
export class F {
static createObject(isB: boolean): A {
if (isB) {
return new B
} else {
return new A
}
}
}
You can't have a circular dependency this way. You can work around by using an interface
itoto.js
import { IToto } from './itoto';
export class Tata implements IToto {
children: Toto[]
}
toto.js
import { Tata } from './tata';
import { IToto } from './itoto';
export class Toto implements IToto{
children: Toto[] = [];
constructor(hasChildren: boolean = false) {
...
}
}
tata.ts
import { IToto } from './itoto';
export class Tata implements IToto {
children: Toto[]
}
这篇关于与角2和SystemJS循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!