我一直遇到各种各样的麻烦。
我有一个实例化的类“client.ts”,它有另一个类作为其结构的一部分 - “address.ts”。在地址我有“addressLocation.ts”,它在这里我需要将“stateShortName”设置为“VIC”我需要在该类上设置一个值,我遇到了麻烦。
客户端类是:
import { Serializable } from "../../../../services/serializable/Serializable"
import { AddressDetails } from "../address/addressDetails"
import { JobDetails } from "../../jobs/jobDetail/jobDetails"
export class ClientDetails implements Serializable<ClientDetails> {
clientId: number;
clientNo: number;
company: boolean;
companyName: string;
abn: string;
isWarrantyCompany: boolean;
requiresPartsPayment: boolean;
clientFirstName: string;
clientLastName: string;
email: string;
mobilePhone: string;
phone: string;
notes: string;
address: AddressDetails = "new AddressDetails";
jobs: JobDetails[];
bankName: string;
bankBSB: string;
bankAccount: string;
active: boolean;
deActivated: string;
activity: boolean;
creatorId: number;
creatorName: string;
dateCreated: string;
updatorId: number;
updatorName: string;
dateUpdated: string;
deserialize(input) {
this.clientId = input.clientId;
this.clientNo = input.clientNo;
this.company = input.company;
this.companyName = input.companyName;
this.abn = input.abn;
this.isWarrantyCompany = input.isWarrantyCompany;
this.requiresPartsPayment = input.requiresPartsPayment;
this.clientFirstName = input.clientFirstName;
this.clientLastName = input.clientLastName;
this.email = input.email;
this.mobilePhone = input.mobilePhone;
this.phone = input.phone;
this.notes = input.notes;
this.bankName = input.bankName;
this.bankBSB = input.bankBSB;
this.bankAccount = input.bankAccount;
this.active = input.active;
this.deActivated = input.deActivated;
this.activity = input.activity;
this.creatorId = input.creatorId;
this.creatorName = input.creatorName;
this.dateCreated = input.dateCreated;
this.updatorId = input.updatorId;
this.updatorName = input.updatorName;
this.dateUpdated = input.dateUpdated;
if (input.jobs) {
this.jobs = new Array<JobDetails>();
for (let count = 0; count < input.jobs.length; count++) {
let job = new JobDetails;
this.jobs.push(job.deserialize(input.jobs[count]));
}
}
if (input.address) {
this.address = new AddressDetails;
this.address.deserialize(input.address);
}
return this;
}
deserializeAddressStateShortName(input) {
}
}
它的地址类设置为“new AddressDetails”。
地址类如下:
import { Serializable } from "../../../../services/serializable/Serializable"
import { AddressLocation } from "./addressLocation"
export class AddressDetails implements Serializable<AddressDetails> {
address1?: string;
address2?: string;
addressLocation: AddressLocation;
deserialize(input) {
//console.log("INPUT: ", input)
this.address1 = input.address1;
this.address2 = input.address2;
if (input.addressLocation) {
this.addressLocation = new AddressLocation;
this.addressLocation.deserialize(input.addressLocation);
}
return this;
}
}
地址将“地址位置”设置为“新地址位置”
import { Serializable } from "../../../../services/serializable/Serializable"
export class AddressLocation implements Serializable<AddressLocation> {
addressLocationId?: number;
suburb?: string;
postcode?: string;
stateShortName?: string;
set stateName(name: string) {
this.stateShortName = name;
}
deserialize(input) {
this.addressLocationId = input.addressLocationId;
this.suburb = input.suburb;
this.postcode = input.postcode;
this.stateShortName = input.stateShortName;
return this;
}
}
每个都可以使用“反序列化”功能进行初始化。
我进行了一次获取,然后使用来自“client”中的fetch的数据,然后填充“address”并随后填充“addressLocation”。
精细。当我获取并使用this.client.deserialize(data)
设置客户端对象并填充对象时,它可以正常工作。
我不想通过整个值集来设置addressLocation中的一个值。
一旦我使用client = new ClientDetails();
创建客户端,我如何在this.client.address.addressLocation.stateShortName
中设置“stateShortName”的值?我不知道如何只是推广这个价值?
如果我正确理解了您的问题,您希望在初始化新类之后和反序列化任何内容之前在成员对象中指定一个值。在这种情况下,您的类成员定义必须是没有address = new AddressDetails;
标志的""
。目前,您的成员变量是一个字符串。