Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 具有Angular 2和SystemJS的循环依赖关系_Typescript_Angular_Systemjs - Fatal编程技术网

Typescript 具有Angular 2和SystemJS的循环依赖关系

Typescript 具有Angular 2和SystemJS的循环依赖关系,typescript,angular,systemjs,Typescript,Angular,Systemjs,我有一个问题,我认为是由循环依赖引起的。经过一些广泛的研究,我还没有找到解决办法。它看起来与这个问题有关:在TypeScript中,但它对我没有帮助 我已经能够简化这个问题了 基本上有三类: 类A,它包含A 继承自A 类F,一个可以根据值创建a或B的工厂 这样做的目的是处理一个可以是动态的参数列表(每个a都是一个参数,可以是一个数组),其中B是a处理文件的专门化。我尝试删除工厂,但只有A和B我仍然得到相同的错误: TypeError:b未定义 加载错误http://localhost:300

我有一个问题,我认为是由循环依赖引起的。经过一些广泛的研究,我还没有找到解决办法。它看起来与这个问题有关:在TypeScript中,但它对我没有帮助

我已经能够简化这个问题了

基本上有三类:

  • A
    ,它包含
    A
  • 继承自
    A
  • F
    ,一个可以根据值创建
    a
    B
    的工厂
这样做的目的是处理一个可以是动态的参数列表(每个
a
都是一个参数,可以是一个数组),其中
B
a
处理文件的专门化。我尝试删除工厂,但只有
A
B
我仍然得到相同的错误:

TypeError:b未定义
加载错误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))
      }
    }
  }
}
import { A } from './a';

export class B extends A {
}
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
    }
  }
}
import { IToto } from './itoto';


export class Tata implements IToto {
  children: Toto[]
}
import { Tata } from './tata';
import { IToto } from './itoto';

export class Toto implements IToto{
  children: Toto[] = [];

  constructor(hasChildren: boolean = false) {
     ...
  }
}
export interface IToto {
  children: Toto[]
}
f.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))
      }
    }
  }
}
import { A } from './a';

export class B extends A {
}
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
    }
  }
}
import { IToto } from './itoto';


export class Tata implements IToto {
  children: Toto[]
}
import { Tata } from './tata';
import { IToto } from './itoto';

export class Toto implements IToto{
  children: Toto[] = [];

  constructor(hasChildren: boolean = false) {
     ...
  }
}
export interface IToto {
  children: Toto[]
}

这样就不能有循环依赖关系。您可以通过使用接口来解决此问题

tata.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))
      }
    }
  }
}
import { A } from './a';

export class B extends A {
}
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
    }
  }
}
import { IToto } from './itoto';


export class Tata implements IToto {
  children: Toto[]
}
import { Tata } from './tata';
import { IToto } from './itoto';

export class Toto implements IToto{
  children: Toto[] = [];

  constructor(hasChildren: boolean = false) {
     ...
  }
}
export interface IToto {
  children: Toto[]
}
toto.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))
      }
    }
  }
}
import { A } from './a';

export class B extends A {
}
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
    }
  }
}
import { IToto } from './itoto';


export class Tata implements IToto {
  children: Toto[]
}
import { Tata } from './tata';
import { IToto } from './itoto';

export class Toto implements IToto{
  children: Toto[] = [];

  constructor(hasChildren: boolean = false) {
     ...
  }
}
export interface IToto {
  children: Toto[]
}
itoto.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))
      }
    }
  }
}
import { A } from './a';

export class B extends A {
}
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
    }
  }
}
import { IToto } from './itoto';


export class Tata implements IToto {
  children: Toto[]
}
import { Tata } from './tata';
import { IToto } from './itoto';

export class Toto implements IToto{
  children: Toto[] = [];

  constructor(hasChildren: boolean = false) {
     ...
  }
}
export interface IToto {
  children: Toto[]
}

另请参见javascript控制台中包含错误消息中提到的
b
的行的位置。实际上,如果类被命名为
toto
tata
,那么错误消息仍然是
TypeError:b是未定义的错误加载http://localhost:3000/app/main.js
Plunker链接对我不起作用。对我也不起作用。。。我将在2分钟内更改链接,我正在做一个没有工厂的plunker。它确实是上面链接的副本。只需将两个类放在同一个文件中就行了,谢谢。这是一个可以工作的plunker,如果它能帮上忙的话:我不明白-它本身就是导入的吗?是的,这很混乱,也不明白…我已经在这篇帖子上贴了一个答案