가져 오기 종속성이 있기 때문에 동일한 클래스가 동일한 .ts 파일에 있습니다.TypeScript의 순환 의존성을 리펙토링하여 별도의 TypeScript 파일을 사용할 수 있습니까?
첫 번째는 추상 GenericModel
:
export abstract class GenericModel {
nodeClass: string;
id: string = "0";
static fromJson(json: any): GenericModel {
if (json.nodeClass === "Entity") {
return EntityModel.fromJson(json);
}
else if(json.nodeClass === "User") {
return UserModel.fromJson(json);
}
return null;
}
}
다른 두 개의 클래스가 EntityModel
를 (아래)하는 사람이 내가 수입의 순환 참조를 제거 할 수 있도록 내가 코드를 리팩토링 도움이 될 수 있다면 감사하겠습니다 및 UserModel
:
export class EntityModel extends GenericModel {
nodeClass: string = "Entity";
alias: string;
description: string;
constructor(public text: string, public id: string, public uuid: string, alias: string, public rand:string = "") {
//[...]
}
//instance methods
//[...]
public static fromJson(json: any): EntityModel {
var entity = new EntityModel(json.text, json.id, json.uuid, json.alias, json.rand);
entity.description = json.description;
if (json.types) {
for (let type of json.types) {
let objectifiedType: EntityModel = EntityModel.fromJson(type);
entity.types.push(objectifiedType);
}
}
if (json.innerEntities){
for (let innerEntity of json.innerEntities) {
let objectifiedInnerEntity: EntityModel = EntityModel.fromJson(innerEntity);
entity.innerEntities.push(innerEntity);
}
}
return entity;
}
}
내가 여기서 뭐하는 거지 것은 nodeClass
에 따라 정적 호출 fromJson()
의 계층 구조를 사용하여 JSON의 직렬화이다.
그것은 GenericModel
는 별도의 파일 인 경우가 별도의 파일에 있다면, 다른 2 명은 GenericModel
를 가져올 필요가하면서 EntityModel
및 UserModel
을 가져와야 것이 분명하다.
EntityModel --- has to import --> GenericModel
UserModel --- has to import --> GenericModel
는 지금뿐만 아니라 클래스는 별도의 .TS 파일에 무엇을 수행하도록 코드를 리팩토링 할 수있는 방법이 있는지 궁금
GenericModel --- has to import --> EntityModel, UserModel
감사합니다.
고마워요! 내가 제안한 공장과 같은 새로운 수업을 추가해야한다고 생각했습니다! 인터페이스/json의 팁 주셔서 감사 :) Btw'''유형 UUID = 번호;'''끝에? 새해 복 많이 받으세요! –
그건 그냥 별칭입니다. 숫자가되는 UID와 같은 것이있을 때, 실제로는 기술적 인 세부 사항이며 자체적 인 의미론이있을 때, 종종 유형 별명을 사용합니다. 이렇게하면 UID 만 사용할 수 있고 실수로 바닐라 번호를 전달하지 않게됩니다. –
나를 위해'''uuid'''는 문자열입니다! 여전히 할 수 있을까요?''type UUID = string;''? 감사합니다 –