Angular 2 和 SystemJS 的循环依赖
Circular dependency with Angular 2 and SystemJS
我有一个问题,我认为是由循环依赖引起的。经过一些广泛的研究,我一直无法找到解决方案。它看起来与这个问题有关:TypeError: b is undefined in __extends in TypeScript,但对我没有帮助。
我已经能够简化 this plunker 中的问题。
基本上有3个classes :
- class
A
,包含 A
的数组
- 继承自
A
的classB
- class
F
,一个可以根据值 创建A
或B
的工厂
这样做的目的是处理一个可以是动态的参数列表(每个A
是一个参数,可以是一个数组),其中B
是[=14=的特化] 来处理文件。我尝试删除工厂,但只有 A
和 B
我仍然得到相同的错误:
TypeError: b is undefined
Error loading http://localhost:3000/app/main.js
这里是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 {
}
和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
}
}
}
你不能以这种方式产生循环依赖。您可以使用接口
解决
tata.ts
import { IToto } from './itoto';
export class Tata implements IToto {
children: Toto[]
}
toto.ts
import { Tata } from './tata';
import { IToto } from './itoto';
export class Toto implements IToto{
children: Toto[] = [];
constructor(hasChildren: boolean = false) {
...
}
}
itoto.ts
export interface IToto {
children: Toto[]
}
另见
我有一个问题,我认为是由循环依赖引起的。经过一些广泛的研究,我一直无法找到解决方案。它看起来与这个问题有关:TypeError: b is undefined in __extends in TypeScript,但对我没有帮助。
我已经能够简化 this plunker 中的问题。
基本上有3个classes :
- class
A
,包含A
的数组
- 继承自
A
的class - class
F
,一个可以根据值 创建
B
A
或B
的工厂
这样做的目的是处理一个可以是动态的参数列表(每个A
是一个参数,可以是一个数组),其中B
是[=14=的特化] 来处理文件。我尝试删除工厂,但只有 A
和 B
我仍然得到相同的错误:
TypeError: b is undefined
Error loading http://localhost:3000/app/main.js
这里是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 {
}
和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
}
}
}
你不能以这种方式产生循环依赖。您可以使用接口
解决tata.ts
import { IToto } from './itoto';
export class Tata implements IToto {
children: Toto[]
}
toto.ts
import { Tata } from './tata';
import { IToto } from './itoto';
export class Toto implements IToto{
children: Toto[] = [];
constructor(hasChildren: boolean = false) {
...
}
}
itoto.ts
export interface IToto {
children: Toto[]
}
另见