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



我使用WebStorm(很棒的IDE(,我正在做一个自定义的宠物项目,它最终将成为一个游戏。但我正在开发一个简单的Alpha版本,向潜在的雇主炫耀,因为我正在找工作,并想把这个项目添加到我的简历中。无论如何,我使用Builder设计模式来创建非常复杂的对象。其中一些对象分布在几个不同的服务中,因为这样做更有意义。我正在制作的游戏将是一款基于文本的RPG游戏,玩家可以在其中创建角色、前往不同地点、收集物品等;超级对象";。。。我得到一个循环依赖性错误。

我试着安装NestJS,因为Nest有办法解决循环依赖性错误,我相信我做得很好,但我仍然会遇到同样的错误。

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

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

终端错误图像

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

决策服务.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的值

第一章服务.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中使用的其他服务

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

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

最新更新