【问题标题】:Circular Dependency Error Preventing Angular From Compiling循环依赖错误阻止 Angular 编译
【发布时间】:2021-01-22 18:04:44
【问题描述】:

我使用 WebStorm(出色的 IDE),并且正在开发我的一个自定义宠物项目,该项目最终将成为一款游戏。但是我正在开发一个简单的 Alpha 版本来向潜在的雇主炫耀,因为我正在寻找工作并希望将这个项目添加到我的简历中。无论如何,我使用 Builder 设计模式来创建非常复杂的对象。其中一些对象分布在几个不同的服务中,因为这样做更有意义。我正在制作的游戏将是一个基于文本的角色扮演游戏,玩家可以在其中创建角色、去不同的位置、收集物品等。所以 IDK 如何解决需要多个 Builder 对象的多个服务,但是当我将它们组合在一起时全部变成一个“超级对象”...我得到一个循环依赖错误。

我尝试安装 NestJS,因为 Nest 可以解决循环依赖错误,我相信我做的一切都是正确的,但我仍然遇到同样的错误。

https://docs.nestjs.com/fundamentals/circular-dependency

正如您在下面看到的,它确实构建了一个 localhost,但当然它什么也没做。

Terminal Error Image

这是来自两个服务文件的一个小示例。 Nest 文档说我需要两个服务文件中的 ForwardRef,而不是我这里的一个。我当然也安装了包@nestjs/common 和@nestjs/core。我还测试了其他几个不依赖于其他服务的 Builder 对象,它们可以很好地显示到控制台。所以我知道我的问题的根源是这些循环依赖。

decisions.service.ts

import { DecisionBuilder } from '../../../../Shared/builder';
import { DecisionType } from '../../../Gameplay/structs';
import { ChapterOneService } from './chapter-one.service';
import { forwardRef, Inject } from '@nestjs/common';

@Injectable({
  providedIn: 'root'
})
export class DecisionsService
{
  commonOne = DecisionBuilder.create({
    decisionType: DecisionType.Common,
    agendaScore: DecisionType.Common.valueOf(),
    agendaImpact: 'Moderate',
    currentPage: this.chapOne.PageOne,
    nextPage: this.chapOne.PageTwo
  });
  commonTwo = DecisionBuilder.create({
    decisionType: DecisionType.Common,
    agendaScore: DecisionType.Common.valueOf(),
    agendaImpact: 'Idealist',
    currentPage: this.chapOne.PageOne,
    nextPage: this.chapOne.PageTwo
  });
  commonThree = DecisionBuilder.create({
    decisionType: DecisionType.Common,
    agendaScore: DecisionType.Common.valueOf(),
    agendaImpact: 'Extremist',
    currentPage: this.chapOne.PageOne,
    nextPage: this.chapOne.PageTwo
  });

  constructor(
    @Inject( forwardRef(() => ChapterOneService) )
    private chapOne: ChapterOneService )
  {

  }

}

上述决策服务仅依赖于另一项服务,即之前的一项。但我使用服务的产品作为currentPagenextPage 的值

chapter-one.service.ts

import { AdventurePageBuilder } from '../../../../Shared/builder';
import { LocationsService } from './locations-service';
import { CharacterService } from '../../character.service';
import { DecisionsService } from './decisions.service';
import {forwardRef, Inject} from '@nestjs/common';

@Injectable({
  providedIn: 'root'
})
/**
 *  CHAPTER ONE
 *  - Chapter Service that contains all Page objects
 *  - Each Chapter component accesses this one service for all content
 */
export class ChapterOneService
{

  PageOne = AdventurePageBuilder.create({
    name: this.location.getShip().area,
    location: this.location.getShip(),
    character: this.character.Krellen,
    storyText: this.pageOneStory(),
    descriptors: this.pageOneDesc(),
    decisionEvents: this.decisions.commonOne
  });
  PageTwo = AdventurePageBuilder.create({
    name: this.location.getShipTwo().area,
    location: this.location.getShipTwo(),
    character: this.character.Krellen,
    storyText: this.pageTwoStory(),
    descriptors: this.pageTwoDesc(),
    decisionEvents: this.decisions.commonOne
  });

  constructor(
    @Inject( forwardRef(() => LocationsService))
    @Inject( forwardRef(() => CharacterService))
    @Inject( forwardRef(() => DecisionsService))
    private location: LocationsService,
    private character: CharacterService,
    private decisions: DecisionsService)
  {

  }

  /***************************************/
  /****************PAGE ONE**************/
  /***************************************/
  getPageOne(): any
  {
    return this.PageOne;
  }

  pageOneStory(): string
  {
    return `${this.PageOne.name} was dark was dreary. Much to ${this.PageOne.character.name}'s dismay`;
  }
  pageOneDesc(): any
  {
    // See if character carries any items with descriptions. Guns, armor, ect.
  }
  /***************************************/
  /****************PAGE TWO***************/
  /***************************************/

  getPageTwo(): any
  {
    return this.PageTwo;
  }

  pageTwoStory(): string
  {
    return `${this.PageTwo.name} was dark was dreary. Much to ${this.PageTwo.character.name}'s dismay`;
  }
  pageTwoDesc(): any
  {
    // See if character carries any items with descriptions. Guns, armor, ect.
  }

  displayHolodeckPage()
  {
    return this.PageOne;
  }

}

上面的一些代码可以忽略,因为我不认为它们是直接的问题...我只是想表明我在这个文件中也使用了 ForwardRef 以及其他使用的服务在chapter-one.service.ts

点击上面的错误图片链接查看我得到的错误代码,但欢迎任何帮助。无论是修复循环错误问题还是重构代码的方法,这样我就可以通过做不同的事情来获得相同的结果。

【问题讨论】:

  • 您确实意识到 Angular 用于前端(读取浏览器)而 NestJS 用于后端(读取 Node 运行时),所以它们的代码库完全不同,对吧?
  • 好的,我删除了 Nest 的东西,我想我找到了问题所在。决策服务和第一章服务是循环逻辑所在。因此,我现在只是将这些属性设为可选,并且注入的其他不使用循环逻辑的服务工作得很好。所以我必须弄清楚如何在不循环的情况下添加这些属性......嗯......

标签: javascript angular typescript nestjs


【解决方案1】:

NestJS 的项目结构和依赖注入系统在某种程度上受到 Angular 的启发,但它们是完全不同的框架。你不能在 Angular 应用程序中使用 NestJS 装饰器并期望它做任何事情。您需要在 Angular 应用程序中解决问题。不要将后端框架依赖项安装到前端应用程序中

【讨论】:

  • 感谢您的建议,我以为我可以做到,哈哈。这是有道理的,为什么它不起作用。我发现问题出在决策服务文件中的属性currentPagenextPage 上。它们依赖于第一章服务,而第一章服务依赖于decisionEvent 属性。所以我只是让道具成为可选的,直到我想出一个解决方法。但我现在让它工作了!
猜你喜欢
  • 2018-05-07
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多