[英]Angular Typescript getter and setter returning Undefined
I have a class where I require a property in that class to return a few fields in an object.我有一个 class,我需要一个 class 中的属性来返回 object 中的一些字段。 I've done this a few times in.Net, but in Angular, I'm battling with an "Undefined" being returned.
我已经在.Net 中这样做了几次,但是在 Angular 中,我正在与返回的“未定义”作斗争。
I can confirm that the properties(transLanguageId, transLangDesc, translation) is populated on the IBatchFile, but not coming back on the GET.我可以确认在 IBatchFile 上填充了属性(transLanguageId、transLangDesc、翻译),但没有在 GET 上返回。 Not even a console.log shows up in the GETTER.
甚至没有一个 console.log 出现在 GETTER 中。 Not hitting the GETTER code I presume.
没有达到我认为的 GETTER 代码。
What am I missing here?我在这里想念什么?
Thank you in advance for your assistance.预先感谢您的协助。
model.ts model.ts
export class translationItem {
id: number;
language: string;
translation: string;
}
export class IBatchFile {
constructor(_transData: translationItem) {
this._transData = new translationItem();
}
private _transData: translationItem;
get transData(): translationItem {
this._transData.id = this.transLanguageId;
this._transData.language = this.transLangDesc;
this._transData.translation = this.translation;
return this._transData;
};
set transData(value: translationItem) {
this._transData.id = value.id;
this._transData.language = value.language;
this._transData.translation = value.translation;
};
transLanguageId: number;
transLangDesc: string;
translation: string;
}
batchdocList.ts批处理文档列表.ts
private _batchFileListResults$ = new BehaviorSubject<IBatchFile[]>([]);
public loadDocList(batchid) {
this.urlservice.getBatchFiles(batchid)
.subscribe(result => {
this._batchFileListResults$.next(result); //**result is of class IBatchFile**
this._batchFileListResults$.value.forEach(item => {
console.log(item.transData); //**returns Undefined**
});
}
url.service.ts url.service.ts
getBatchFiles(batchId: number) {
return this.dataservice.getBatchFiles(config.resources.Api.gatewayUri + config.resources.Api.getBatchFiles+"/"+batchId);
}
data.service.ts数据服务.ts
getBatchFiles(url:string): Observable<IBatchFile[]> {
return this.http.get<IBatchFile[]>(url)
.pipe(map(response => response))
.pipe(catchError(this.handleError));
}
Assuming that:假如说:
@angular/common/http
):@angular/common/http
):class X
class X
you need to know that it to know that http.get<X>
does not return an instance of X
class.您需要知道它才能知道
http.get<X>
不会返回X
class 的实例。
The fields are correctly populated, but the prototype chain is not.字段已正确填充,但原型链未正确填充。 You can test it yourself (in your browser DevTools):
你可以自己测试它(在你的浏览器 DevTools 中):
result.constructor //ƒ Object()
Compare it with:将其与:
const x = new X();
x.constructor //class X
Thus, any items that are placed on the objects prototype (like methods and get / set accessors) are missing in the result object.因此,结果 object 中缺少放置在对象原型上的任何项目(如方法和 get/set 访问器)。
In my project I restricted return types from HttpClient.get to types ( type X
instead of class X
).在我的项目中,我将 HttpClient.get 的返回类型限制为类型(
type X
而不是class X
)。 As types are compiled away, you will avoid this kind of weirdness.随着类型被编译掉,您将避免这种怪异。
Alternatively, you can transform the output from HttpClient.get and instantiate real classes via contructor.或者,您可以从 HttpClient.get 转换 output 并通过构造函数实例化真实类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.