简体   繁体   English

防止 Angular 编译的循环依赖错误

[英]Circular Dependency Error Preventing Angular From Compiling

I use WebStorm (great IDE) and I'm working on a custom pet project of mine which will become a game eventually.我使用 WebStorm(很棒的 IDE)并且我正在开发我的自定义宠物项目,该项目最终将成为游戏。 But I'm developing a bare bones Alpha version to show off to potential employers because I'm looking for work and want to add this project to my resume.但我正在开发一个简单的 Alpha 版本以向潜在雇主炫耀,因为我正在寻找工作并希望将此项目添加到我的简历中。 Anyway, I'm using the Builder design pattern to create very complex objects.无论如何,我正在使用 Builder 设计模式来创建非常复杂的对象。 Some of these objects are spread across several different services because it makes more sense to do it that way.其中一些对象分布在几个不同的服务中,因为这样做更有意义。 The game I'm making is going to be a text-based RPG where players can create characters, go to different locations, gather items, etc. So IDK how to get around needing multiple services which multiple Builder objects, but when I combined them all into a "super object"...I get a circular dependency error.我正在制作的游戏将是一个基于文本的 RPG 游戏,玩家可以在其中创建角色、去不同的位置、收集物品等。 所以 IDK 如何解决需要多个 Builder 对象的多个服务,但是当我将它们组合起来时全部变成“超级对象”...我收到循环依赖错误。

I tried installing NestJS because Nest has a way around Circular Dependency errors and I believe I did everything right, but I'm still getting the same error.我尝试安装 NestJS,因为 Nest 有解决循环依赖错误的方法,我相信我做的一切都是正确的,但我仍然遇到相同的错误。

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

As you can see below, it does build a localhost, but of course it doesn't do anything.正如你在下面看到的,它确实构建了一个本地主机,但当然它什么都不做。

Terminal Error Image终端错误图像

Here's a small example from two Service files.这是来自两个服务文件的一个小例子。 The Nest docs say I need the ForwardRef in both Services files, not one which I have here. Nest 文档说我在两个服务文件中都需要 ForwardRef,而不是我这里有的一个。 I've also of course installed the packages @nestjs/common and @nestjs/core.我当然也安装了包@nestjs/common 和@nestjs/core。 I've also tested a couple other Builder objects that don't depend on another service and they display to the console just fine.我还测试了几个不依赖于其他服务的其他 Builder 对象,它们在控制台上显示得很好。 So I know the source of my problem are these Circular Dependencies.所以我知道我的问题的根源是这些循环依赖。

decisions.service.ts Decision.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 )
  {

  }

}

The above Decision Service only depends on one other service and that's the one before.上述决策服务仅依赖于其他一项服务,而这就是之前的一项。 But I use the product of the service as a value for currentPage and nextPage但是我使用服务的产品作为currentPagenextPage的值

chapter-one.service.ts第一章.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;
  }

}

Some of the code from above can be ignored as I don't think they're directly the problem...I just wanted to show that I use the ForwardRef in this file too as well as the other Services that are used in chapter-one.service.ts上面的一些代码可以忽略,因为我不认为它们直接是问题......我只是想表明我也在这个文件中使用了 ForwardRef 以及在chapter-one.service.ts中使用的其他服务chapter-one.service.ts

Click on the error image link above to see the error code I get, but any help is welcomed.单击上面的错误图像链接查看我得到的错误代码,但欢迎任何帮助。 Whether it's a fix to the circular error problem or a way to refactor the code so I can essentially get the same result by doing something differently.无论是修复循环错误问题还是重构代码的方法,我基本上都可以通过做不同的事情来获得相同的结果。

NestJS has project structure and Dependency Injection system that is somewhat inspired by Angular but they are completely different frameworks. NestJS 有项目结构和依赖注入系统,有点受 Angular 的启发,但它们是完全不同的框架。 You cannot use NestJS decorators inside of an Angular app and expect it to do anything.您不能在 Angular 应用程序中使用 NestJS 装饰器并期望它做任何事情。 You need to fix the problem in your Angular app.您需要修复 Angular 应用程序中的问题。 Don't install backend framework dependencies into a frontend app不要将后端框架依赖项安装到前端应用程序中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM