角度4的循环依赖

时间:2017-09-23 12:56:39

标签: angular service module warnings circular-dependency

每当我运行项目时,我都会收到以下警告:

  

检测到循环依赖关系:src / app / Dashboard / dashboard.module.ts - >   src / app / Dashboard / finance.service.ts - >   SRC /应用/仪表板/ dashboard.module.ts

在我的finances.service中,我有一个函数创建一个“Statistics”类型的对象,这是我在dashboard上定义的类。模块

dashboard.module

page_soup1 = soup(page_html1, "html.parser")

tablecontainer = page_soup1.find_all("tr")

for container in tablecontainer:

    Z = container1.find('a', {'href': lambda x : x.startswith('/NSN/')})

    print(Z)

finance.service

export class Stadistics {
  mod: number;
  min: number;
  max: number;
}

我也尝试过使用:const stats:Statistics = new Statistics;但我一直得到同样的警告。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

在这种情况下的标准方法是将导致循环依赖的声明移动到单独的源文件中。所以,它看起来像这样:

<强> statistics.class.ts

export class Statistics {
    mod: number;
    min: number;
    max: number;
}

<强> finance.service.ts

import {Statistics} from './statistics.class';
....
....
....
getStatistics(array: Array<number>) {
    const stats = new Statistics();
    stats.max = 0;
    stats.min = 0;
    stats.mod = 0;
}

<强> dashboard.module.ts

import {Statistics} from './statistics.class';

// use it here where you need it.

当然,您需要仔细考虑如何分割您的资源,以便它有意义。