Javascript 循环依赖项错误阻止Angular编译
我使用WebStorm(伟大的IDE),我正在做我的一个定制宠物项目,它最终将成为一个游戏。但是我正在开发一个简单的Alpha版本来向潜在的雇主炫耀,因为我正在找工作,想把这个项目添加到我的简历中。无论如何,我使用Builder设计模式来创建非常复杂的对象。其中一些对象分布在几个不同的服务中,因为这样做更有意义。我正在制作的游戏将是一个基于文本的RPG,玩家可以在其中创建角色、前往不同的位置、收集项目等。因此,IDK如何绕过需要多个构建器对象的多个服务,但当我将它们组合成一个“超级对象”时……我遇到了一个循环依赖性错误 我试着安装NestJS,因为Nest有一种绕过循环依赖错误的方法,我相信我做的一切都是对的,但我仍然会遇到同样的错误 正如您在下面看到的,它确实构建了一个本地主机,但当然它什么都不做 下面是来自两个服务文件的一个小示例。Nest文档说我需要这两个服务文件中的ForwardRef,而不是这里的一个。当然,我还安装了@nestjs/common和@nestjs/core包。我还测试了另外两个不依赖于其他服务的构建器对象,它们在控制台上显示得很好。所以我知道问题的根源是这些循环依赖 决策.服务.tsJavascript 循环依赖项错误阻止Angular编译,javascript,angular,typescript,nestjs,Javascript,Angular,Typescript,Nestjs,我使用WebStorm(伟大的IDE),我正在做我的一个定制宠物项目,它最终将成为一个游戏。但是我正在开发一个简单的Alpha版本来向潜在的雇主炫耀,因为我正在找工作,想把这个项目添加到我的简历中。无论如何,我使用Builder设计模式来创建非常复杂的对象。其中一些对象分布在几个不同的服务中,因为这样做更有意义。我正在制作的游戏将是一个基于文本的RPG,玩家可以在其中创建角色、前往不同的位置、收集项目等。因此,IDK如何绕过需要多个构建器对象的多个服务,但当我将它们组合成一个“超级对象”时……我
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 )
{
}
}
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;
}
}
上面的决策服务只依赖于一个其他服务,即之前的服务。但是我使用服务的产品作为currentPage
和nextPage
第一章.服务.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 )
{
}
}
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以及在第一章.service.ts中使用的其他服务
单击上面的错误图像链接查看我得到的错误代码,但欢迎提供任何帮助。无论是解决循环错误问题还是重构代码的方法,我都可以通过做一些不同的事情从本质上获得相同的结果。NestJS的项目结构和依赖注入系统在某种程度上受到Angular的启发,但它们是完全不同的框架。你不能在Angular应用程序中使用NestJS装饰器,并期望它做任何事情。您需要修复Angular应用程序中的问题。不要将后端框架依赖项安装到前端应用程序中NestJS的项目结构和依赖项注入系统在某种程度上受到Angular的启发,但它们是完全不同的框架。你不能在Angular应用程序中使用NestJS装饰器,并期望它做任何事情。您需要修复Angular应用程序中的问题。不要将后端框架依赖项安装到前端应用程序中你知道Angular用于前端(阅读浏览器)而NestJS用于后端(阅读节点运行时),因此它们的代码库完全不同,对吗?好吧,我删除了Nest的内容,我想我已经解决了问题。决策服务和第一章服务是循环逻辑的所在。因此,我现在只是将这些属性设置为可选的,而注入的其他不使用循环逻辑的服务工作得很好。所以我必须弄清楚如何在不循环的情况下添加这些属性…嗯…你知道Angular用于前端(读取浏览器)而NestJS用于后端(读取节点运行时),所以它们有完全不同的代码库,对吗?好吧,我删除了Nest,我想我解决了问题。决策服务和第一章服务是循环逻辑的所在。因此,我现在只是将这些属性设置为可选的,而注入的其他不使用循环逻辑的服务工作得很好。所以我必须弄清楚如何在没有循环的情况下添加这些属性…嗯…谢谢你的建议,我想我可以这样做lol。不过这很有道理为什么它不起作用。我发现问题在于决策服务文件中的属性currentPage
和nextPage
。它们依赖于第一章服务,而第一章服务依赖于decisionEvent
属性。所以我只是让道具可选,直到我找到解决办法。但我现在已经开始工作了!谢谢你的建议,我想我可以做到,哈哈。这是有道理的,为什么它不起作用。我发现问题在于决策服务文件中的属性currentPage
和nextPage
。它们依赖于第一章服务,而第一章服务依赖于decisionEvent
属性。所以我只是让道具可选,直到我找到解决办法。但我现在已经开始工作了!