我使用WebStorm(出色的IDE),正在从事我的自定义宠物项目,该项目最终将成为游戏。但是我正在开发Alpha版本,以向潜在的雇主炫耀,因为我正在寻找工作,并希望将此项目添加到我的简历中。无论如何,我正在使用Builder设计模式来创建非常复杂的对象。其中一些对象分布在几个不同的服务中,因为这样做更有意义。我正在制作的游戏将是一个基于文本的RPG,玩家可以在其中创建角色,前往不同位置,收集物品等。因此IDK如何解决需要多个Builder对象提供的多种服务,但是当我将它们组合时都变成一个“超级对象” ...我得到一个循环依赖错误。
我之所以尝试安装NestJS,是因为Nest可以解决循环依赖错误,并且我相信我所做的一切都正确,但是我仍然遇到相同的错误。
https://docs.nestjs.com/fundamentals/circular-dependency
正如您在下面看到的,它确实建立了一个本地主机,但是它当然什么也没做。
这是来自两个Service文件的一个小示例。 Nest文档说,我在两个Services文件中都需要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 )
{
}
}
上述决策服务仅依赖于另一项服务,这就是之前的一项。但是我将服务的乘积用作currentPage
和nextPage
第章一章.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
单击上面的错误图像链接以查看我得到的错误代码,但是欢迎您提供任何帮助。不管是解决循环错误问题还是重构代码的方法,这样我基本上都可以通过做不同的事情来获得相同的结果。
答案 0 :(得分:2)
NestJS具有项目结构和Dependency Injection系统,该系统在某种程度上受到Angular的启发,但它们是完全不同的框架。您不能在Angular应用程序内使用NestJS装饰器,并且不能期望它做任何事情。您需要在Angular应用中解决问题。不要将后端框架依赖项安装到前端应用程序中