javascript - 循环依赖错误阻止 Angular 编译
问题描述
我使用 WebStorm(伟大的 IDE),我正在开发我的一个自定义宠物项目,最终将成为一个游戏。但是我正在开发一个简单的 Alpha 版本来向潜在的雇主炫耀,因为我正在寻找工作并且想将这个项目添加到我的简历中。无论如何,我使用 Builder 设计模式来创建非常复杂的对象。其中一些对象分布在几个不同的服务中,因为这样做更有意义。我正在制作的游戏将是一个基于文本的 RPG,玩家可以在其中创建角色、去不同的位置、收集物品等。所以 IDK 如何解决需要多个 Builder 对象的多个服务,但是当我将它们组合起来时全部进入“超级对象”......我得到一个循环依赖错误。
我尝试安装 NestJS,因为 Nest 有办法解决循环依赖错误,我相信我做的一切都是正确的,但我仍然遇到同样的错误。
https://docs.nestjs.com/fundamentals/circular-dependency
正如您在下面看到的,它确实构建了一个 localhost,但它当然什么也没做。
这是来自两个服务文件的一个小示例。Nest 文档说我需要两个服务文件中的 ForwardRef,而不是我这里的一个。我当然也安装了包@nestjs/common 和@nestjs/core。我还测试了其他几个不依赖于其他服务的 Builder 对象,它们可以很好地显示在控制台上。所以我知道我的问题的根源是这些循环依赖。
决策.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
单击上面的错误图像链接以查看我得到的错误代码,但欢迎提供任何帮助。无论是修复循环错误问题还是重构代码的方法,这样我就可以通过做不同的事情来获得相同的结果。
解决方案
NestJS 具有受 Angular 启发的项目结构和依赖注入系统,但它们是完全不同的框架。你不能在 Angular 应用程序中使用 NestJS 装饰器并期望它做任何事情。您需要在 Angular 应用程序中解决问题。不要将后端框架依赖项安装到前端应用程序中
推荐阅读
- jms - apache active mq消息已出队,但未调用@JmsListener的方法
- cobbler - 使用 cobbler(kickstart 文件)配置第二个接口并分配静态 IP
- visual-studio-code - 如何在 VSCode 中禁用代码片段的 Enter 按钮
- python - 通过在 numpy/sparse 中添加 n^dim 元素进行下采样
- buildbot - BuildBot:允许的失败
- python - 打开 edx-ora2 定制
- c++ - 将文本添加到 json C++
- parquet - 实木复合地板中的 OffsetIndex 1.11.0
- c - 已声明但从未定义的不透明结构
- php - 如何仅在备用输入字段行上显示数据?