与角2和SystemJS循环依赖 [英] Circular dependency with Angular 2 and SystemJS

查看:171
本文介绍了与角2和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
    }
  }
}


解决方案

您不能有一个循环依赖这种方式。您可以通过使用一个接口解决

Plunker例如

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 of A
  • the class B, that inherits from A
  • the class F, a factory that can create an A or a B 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

Plunker example

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆