【问题标题】:Circular dependency detected between unrelated functions在不相关的函数之间检测到循环依赖
【发布时间】:2020-04-09 16:06:18
【问题描述】:

我在使用两个实用程序文件中单独导出的函数的 Angular 应用程序中遇到循环依赖检测警告。我将展示下面两个文件的简化示例。

第一个文件:

// type-helper.ts
import {getPropertyIfExists} from "./helper";

export function isType(item: any, type: string, property?: string) {
  const target = getPropertyIfExists(item, property);
  if (typeof type !== "string") return false;
  else return typeof target === type;
}

export function isArray(item: any[] | any): item is any[] {
  return item && Array.isArray(item);
}

export function isObject(item: object | any): item is object {
  return item && typeof item === 'object';
}

第二个文件:

// helper.ts
import {isArray, isObject} from "./type-helper";

export function getPropertyIfExists(item: any, property?: string): any {
  if (property) return item[property];
  else return item;
}

export function removeUndefinedProperties<T>(obj: T): T {
  const keys = Object.keys(obj);
  for (const key of keys) {
    if (isArray(obj[key])) obj[key] = removeEmptyValuesFromArray(obj[key]);
    else if (isObject(obj[key])) obj[key] = removeUndefinedProperties(obj[key]);
    else if (item === undefined || item === null) delete obj[key];
  }
  return obj;
}

这两个文件都提供了我喜欢在我的应用程序中重复使用的小型实用程序,而不必链接到单个服务。据我所知,这些文件之间没有其他链接。

所以,我的问题:

  1. 警告是预期的行为吗?如果您在组件和服务之间存在循环依赖关系,我理解警告,其中导入了整个类,但在这种情况下,实际导入的项目之间没有循环依赖关系。
  2. 如果没有问题,有没有办法让这种特定情况下的警告消失(不从其他循环依赖项中删除警告)?

谢谢!

【问题讨论】:

  • 可以显示这些文件所在的文件夹结构吗?
  • 它们都在“/src/app/utilities”文件夹中。

标签: angular typescript circular-dependency


【解决方案1】:

实用程序文件夹中的 index.ts 是一个桶模块(桶是一种将多个模块的导出汇总到一个方便的模块中的方法。桶本身是一个模块文件,它重新导出其他模块的选定导出。)

您不能导入桶模块本身。 因为会出现循环依赖。 (将无限循环导入)

index.ts
  type-helper.ts
  helper.ts

helper.ts -> index.ts -> type-helper.ts
                      -> helper.ts -> index.ts -> type-helper.ts
                                               -> helper.ts

为防止这种情况,您需要导入确切的补丁

// helper.ts
// import {isArray, isObject} from "./helper";
import {isArray, isObject} from "./helper/type-helper";

export function getPropertyIfExists(item: any, property?: string): any {
  if (property) return item[property];
  else return item;
}

export function removeUndefinedProperties<T>(obj: T): T {
  const keys = Object.keys(obj);
  for (const key of keys) {
    if (isArray(obj[key])) obj[key] = removeEmptyValuesFromArray(obj[key]);
    else if (isObject(obj[key])) obj[key] = removeUndefinedProperties(obj[key]);
    else if (item === undefined || item === null) delete obj[key];
  }
  return obj;
}

你可能有 helper.ts 而不是 index.ts

都是一样的

helper.ts -> type-helper.ts -> helper.ts -> type-helper.ts ...

将 getPropertyIfExists() 函数移入 type-helper.ts

【讨论】:

  • 感谢您的回答!只是为了让我清楚,您是说自动生成一个 index.ts/barrel 文件吗?我自己没有创建任何桶文件,我只是直接从文件引用中导入函数。话虽如此,我注意到简化代码错误地显示了导入“./helper”的帮助文件。它实际上是从“./type-helper”导入函数。
  • index.ts需要自己创建,不会自动创建
  • 这就是我不确定的地方,如果我将文件中的特定函数导入另一个函数,它是否会将该文件中的所有其他不相关的依赖项与该函数一起导入?因为“getPropertyIfExists”函数不需要导入,我假设导入它与简单地将该函数复制并粘贴到文件中而不用它拉任何其他导入大致相同。
  • 一个文件中的所有函数都导出到一个模块中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 2013-08-19
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多